Hi,

Is it possible to have an Inline C function return to perl a reference to a struct - then pass that reference back to another Inline C function which then, say, prints out the values of the various members of the struct ?

Faik it may well be trivial, but I've not yet found a way of doing it.

Here is my attempt:
--------------------------------------------------------------
use warnings;

use Inline (C => Config =>
            BUILD_NOISY => 1,
            );

use Inline C => <<'EOC';

     typedef struct {
       SV * suit;
       int value;
       } Card;

SV * create_struct(SV * s, int v) {
     Card c, * cptr;
     cptr = &c;
     c.suit = s;
     c.value = v;
     printf("%s %d\n", SvPV_nolen(cptr->suit), cptr->value);
     return sv_setref_pv(newSViv(0), Nullch, cptr);

}

void deref_ref(SV * p) {
     Card * d;
     d = (Card *) SvIV(SvRV(p));
     printf("%s %d\n", SvPV_nolen((*d).suit), (*d).value);

}


EOC


$x = 'Hearts';
$y = 7;

print "$x $y\n";
$p = create_struct($x, $y);
deref_ref($p);
--------------------------------------------------------------

That prints:
Hearts 7
Hearts 7
SCALAR(0x2caff00) 44561140

The OO example in Inline::C-Cookbook suggests to me that 'deref_ref()' could be rewitten as:

void deref_ref(SV * p) {
printf("%s %d\n", ((Card *)SvIV(SvRV(p)))->suit,
                  ((Card *)SvIV(SvRV(p)))->value);
}

But that doesn't quite work either - it prints gibberish for 'suit', and the same 44561140 for 'value'.

Maybe I've got 'create_struct()' returning something inappropriate ?

Thanks for any help.

Cheers,
Rob

--
Any emails containing attachments will be deleted from my ISP's mail server before I even get to see them. If you wish to email me an attachment, please provide advance warning so that I can make the necessary arrangements.




Reply via email to