On Thu, 22 Feb 2001, Kort, Eric wrote:
> 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);
Dereferencing a null value is not good.
# boom
printArray($array, 10);
# or boom
delete $array->[2];
printArray($array, 5);
> __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));
As an alternative, it looks like setting the third argument to TRUE makes
non-existing array elements auto-vivify, so a SV** is always returned:
SV_arrayElement = SvIV(*av_fetch(AV_inputArray, i, TRUE));
But that will generate "Use of unitialized value..." warnings with -w on.
> printf ("Element %1d of your array is %1d\n", i, SV_arrayElement);
> }
> }
--
Tim Gim Yee
[EMAIL PROTECTED]