Basically I am trying to simulate C pointer dereference using gnu-prolog.
Here is the code:
Prolog
:-foreign(fun(+integer,-integer)).
% p = &b;
testfun(Var, Val) :- fun(Val, Var).
main :-
A is 1,
testfun(A, P),
write(P),
C:
#include <gprolog.h>
#include <string.h>
PlBool fun(int ptr, int* res){
*res = &ptr; // this is wrong
if(res==NULL){
return PL_FALSE;
}else{
return PL_TRUE;
}
}
So basically it is wrong, because ptr is just a temp variable on the stack, and its memory will be deallocated after the calling fun.
So my question is, is it possible to get the variable's memory addresses in gnu prolog (For example, in this case, it is the address of A, not the address of ptr)?