Re: access list elements from C code
Daniel Diaz <[email protected]>
| Newsgroups | gmane.comp.gnu.prolog.general |
|---|---|
| Message-ID | <[email protected]> |
Le 23/06/2012 13:57, dmaugis a écrit :
> Well...I m trying to access list element from C code, and obviously there is
> something I miss-understood, because I crash. any help/explanation would be
> welcome.
>
> On the gprolog side I have:
>
> :-foreign(sdl_init(+term)).
>
> initialise :-
> sdl_init([timer,audio,video]).
>
> On the C side:
>
> int sdl_init(PlTerm list)
> {
> unsigned long flags=0;
> if(Pl_Builtin_List(list)) {
> PlTerm *terms=NULL;
> int len=Pl_List_Length(list);
> terms=malloc(sizeof(PlTerm)*len);
> if(terms) {
> unsigned i=Pl_Rd_Proper_List_Check(list, terms);
> for(i=0; i<len; i++) {
> char* s;
> switch(Pl_Type_Of_Term(terms[i])) {
> case PL_INT:
> printf("integer.\n");
> flags|=Pl_Rd_Integer(terms[i]);
> break;
> case PL_ATM:
> s=Pl_Atom_Name(terms[i]);
> printf("atom '%s'\n",Pl_Is_Valid_Atom(terms[i]) ? "valid":"invalid");
> break;
> }
> }
>
> And I get:
>
> | ?- go.
> atom 'invalid'
> Fatal Error: Segmentation Violation
>
> it seems when doing :
>
> s=Pl_Atom_Name(terms[i]);
>
>
>
Your code is incomplete and cannot be compiled... Next time, please
provide a usable code...
However there is clearly an error here:
s=Pl_Atom_Name(terms[i]);
printf("atom '%s'\n",Pl_Is_Valid_Atom(terms[i]) ? "valid":"invalid");
Because you don't "obtain" the atom. Use Pl_Rd_Atom() for this. Example:
int atom = Pl_Rd_Atom(terms[i]);
s=Pl_Atom_Name(atom);
printf("atom %s is '%s'\n",s,Pl_Is_Valid_Atom(Pl_Rd_Atom(terms[i])) ?
"valid":"invalid");
Daniel
--
Ce message a ete verifie par MailScanner
pour des virus ou des polluriels et rien de
suspect n'a ete trouve.