Prolog can get or modify the contents of a foreign term with the procedures
get_contents(+Datum, +Part, -Value) get_contents(+Datum, *Part, *Value) put_contents(+Datum, +Part, +Value).
It can also get a pointer to a field or element of a foreign term with the procedure
get_address(+Datum, +Part, -Value). get_address(+Datum, *Part, *Value).
For all three of these, Datum must be a foreign term, and
Part specifies what part of Datum Value is. If
Datum is an array, Part should be an integer index into
the array, where 0 is the first element. For a pointer, Part
should be the atom
contents and Value will be what the pointer
points to. For a struct, Part should be a field name, and
Value will be the contents of that field. In the case of
get_address/3, if Part is unbound, then
get_contents/3 will backtrack through all the valid parts of
Datum, binding both Part and Value. A C programmer
might think of the following pairs as corresponding to each other:
get_contents(Foo, Bar, Baz) Baz = Foo->Bar put_contents(Foo, Bar, Baz) Foo->Bar = Baz get_address(Foo, Bar, Baz) Baz = &Foo->Bar.
The hitch is that only atomic and pointer types can be got and put
put_contents/3. This is because Prolog can
only hold pointers to C structures, not the structures themselves.
This isn't quite as bad as it might seem, though, since usually
structures contain pointers to other structures, anyway. When a
structure directly contains another structure, Prolog can get a
pointer to it with
Access to most fields is accomplished by peeking into memory (see ref-ari-aex-pee), so it is very efficient.