C value boxes - Factor Documentation (original) (raw)

Sometimes it is useful to create a byte array storing a single C value, like a struct with a single field. A pair of utility words exist to make this more convenient:
( value c-type -- c-ptr )
deref ( c-ptr c-type -- value )

These words can be used to in conjunction with, or instead of, with-out-parameters to handle "out-parameters". For example, if a function is declared in the following way:

FUNCTION: int do_foo ( int* a )

and writes to the pointer 'a', then it can be called like this:

1234 int [ do_foo ] keep int deref

The stack will then contain the two integers emitted by the 'do_foo' function.