I really don't have much experience with inline::C, but I took a look at your problem. It appears to me that you really don't want an "always mapping" of undef to NULL pointer because maybe undef in another context might mean a missing int in an array of ints or something else. Here in this particular case, undef means a NULL C pointer to char and I think you have to check for that explicitly.

I wrote some simple test code below. In this case, I used SV* instead of char*. The C code figures out if the SV contains a valid pointer to string or not? I tested with both NULL undef and "asdf" as test inputs - see below. I suspect that something is missing in the case of UTF-8 strings. Also my simple test probably should be more complex because say passing an int would be treated the same as sending undef (the test is just valid string or not?).

Hope that this is on the right track...
Update: I guess the obvious was unsaid: why not use "" instead of undef? A null string is not undef.

use v5.10; use Inline 'C'; say "At least we have compiled a bit of C code!"; my $ret= "HD3"; my $output = enctypex_msname("HD2", undef); say "Output from enctypex_msname() call: '$output', \$ret is '$ret'"; =OUTPUT CASE 1: with: my $output = enctypex_msname("HD2", "asdf"); At least we have compiled a bit of C code! Output from enctypex_msname() call: 'HD2', $ret is 'HD3' HD2 asdf name 0000000002D2BC58 retname 0000000002D2BB68 CASE 2: my $output = enctypex_msname("HD2", undef); At least we have compiled a bit of C code! Output from enctypex_msname() call: 'HD2', $ret is 'HD3' HD2 (null) name 0000000002C93298 retname 0000000000000000 =cut __END__ __C__ unsigned char *enctypex_msname(char *name, SV* retname) { char * retname_string = SvPOK(retname) ? SvPV_nolen(retname) : NUL +L; printf ("%s %s\n",name,retname_string); printf ("%s %p %s %p\n", "name ",name, "retname ",retname_string); return name; }
PS again on the C code itself:  "static unsigned char    msname[256];"could be trouble. There is only one msname array. If you call this function again, your code will return the same address for msname as it did the last time. Also perhaps if(!name) return(NULL); should be if(! *name) return(NULL); "" does not mean NULL it means pointer to array of char, if first char is \0 then string is "empty" - not the same as a NULL pointer.

In reply to Re: Inline::C and NULL pointers by Marshall
in thread Inline::C and NULL pointers by markong

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.