> -----Original Message-----
> From: Tim Gim Yee [mailto:[EMAIL PROTECTED]]
> 
> On Wed, 21 Feb 2001, Jack H. Stanley wrote:
<SNIP> 
> >   svp = av_fetch(array, i, FALSE);
> >   printf("ARRAY[x][%d] = %s\n",i,svp);
> 
> Not a C/XS programmer, but it looks like you need to 
> dereference svp to a
> SV*, then you can try to get at the string value.
> 
>     if (svp == NULL) {
>         printf("ARRAY[x][%d] = undef\n", i);
>     } else {
>         printf("ARRAY[x][%d] = %s\n", i, SvPV_nolen(*svp));
>     }

Right.  av_fetch returns a SV**, not SV* (perhaps some knowledgeable
programmer can describe why this is a desirable behavior).

Another approach is to dereference at the time of assignment, thusly:

SV_arrayElement = SvIV(*av_fetch(array, i, FALSE));

As in:

#!/usr/bin/perl
use Inline C;

$array = [8,4,5,1,2];
printArray($array, 5);
exit;

__END__

__C__

void printArray(SV* RV_inputArrayRef, int i_numElements)
{
  AV* AV_inputArray;
  SV* SV_arrayElement;  
  int       i;
  
  AV_inputArray = (AV*)SvRV(RV_inputArrayRef);

  for (i=0; i < i_numElements; i++)
  {
    SV_arrayElement = SvIV(*av_fetch(AV_inputArray, i, FALSE));
    printf ("Element %1d of your array is %1d\n", i, SV_arrayElement);
  }
}


hth,
Eric

Reply via email to