gants has asked for the wisdom of the Perl Monks concerning the following question:

#include <stdio.h> int tst(int len,char str[1024]){ printf("called c from perl len:'%i' str:'%s' \n",len, str); return 1; } int main( ){ tst(54,"char"); return 1; }
then what's wrong here, in C it works well, module for perl
compiled well, but this use TST; TST::test(4444,"ss"); prints called c from perl len:'4444' str:'(null)'

Replies are listed 'Best First'.
Re: usage libc in perl, how using char's?
by Corion (Patriarch) on Apr 29, 2010 at 08:06 UTC

    How are you linking your Perl code to the C code? Are you using Inline::C? Otherwise, if you pass in a Perl scalar, you will need to use the appropriate macros to get at the data payload. To get at the string part of a scalar, you need to use

    char* strval = SvPV(str);

    I recommend looking at illguts to learn about the low level Perl data structures.

Re: usage libc in perl, how using char's?
by samarzone (Pilgrim) on Apr 29, 2010 at 08:34 UTC

    I don't know much about linking Perl and C but there is one thing which got my attention.

    Your function name is tst in C while it is test in perl. Is is the way it should be?

      char* strval = SvPV(str);

      that's been wrong I did C-module to PERL, it's opposite using PERL in C-lang. that's my source which I'm using as C-module to PERL.
      int tst(int len,char **str) { printf("called c from perl len:'%i' str:'%s' \n", len, str); return 1; }

      Your function name is tst in C while it is test in perl. Is is the way it should be?

      no, of course not, because I've posted there different outputs from just c-programm and module compiled to perl and called from perl, above...