C value boxes
Factor handbook » C library interface » Passing data between Factor and C

Prev:Output parameters in C
Next:Byte arrays and the garbage collector

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:
<ref> ( 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 <ref> [ do_foo ] keep int deref

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