Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pass array as a parameter to a function (Perl & C)

Status
Not open for further replies.

darkskin

Programmer
Feb 10, 2004
2
DE
Hello all.

I have a problem here and don't seem to find a solution to it.
I have this code in C and a script in Perl that calls a function from the C code.
Now, the thing is that the C function looks something like this:
CalcMap (uword index, const tMainMap * pst_Par)

This tMainMap structure contains 3 elements: x1, x2, x3.
Now, I made an array of three elements to substitute the structure: test_array
and also a function in perl that substitutes the function in C:
test_func(short par0, short test_array)
{
return (CalcMap( par0, ((const tMainMap *)test_array)));
}

Now, in my perl script, I have 3 for loops in which I change the values for the 3 elements of the array and another for loop in which I change the value of the "index" parameter of the C function, and then I call the function:
test_func ($set_index, @test_array);

And here I get a very "nice" error:
Use of inherited AUTOLOAD for non-method main::test_func() is deprecated at D:\test\test_CalcMap.pl line 163.
Can't locate auto/main/test_func.al in @INC (@INC contains: D:\test\_Inline\lib D:/Perl/lib D:/Perl/site/lib .) at D:\test\test_CalcMap.pl line 163

Can anyone help me out with this?!

I have the feeling I cannot pass an array as a parameter to a function but only to subs... am I right?! And if this is actually the problem can somebody tell me another way of getting this solved?!

Thanks in advance!!
darkskin
 
I've never written anything in XS, have you read all the manpages on the subject? I'm pretty sure there are plenty and bets are good an answer's in them.

Shot in the dark: Have you tried passing the array by \@reference?

________________________________________
Andrew - Perl Monkey
 
Thanks Andrew for your response.

I tried passing the array by reference... no good.

I found though a work around way... I'll put it here in case anyone is interested in seeing a solution to the problem...
So here it goes:

#! d:/perl/bin/perl
use warnings;
use Inline "C";

my @test_array = (0,0,0);
my $i0 = 0;
my $i1 = 0;
my $i2 = 0;
my $i3 = 0;
my $setval0 = 0;
my $setval1 = 0;
my $setval2 = 0;
my $setval3 = 0;

my @X1_array = (value1, value2, value3,...);
my @X2_array = (value1, value2, value3,...);
my @X3_array = (value1, value2, value3,...);
my @index_array = (value1, value2, value3,...);

$i0 = 0;
for (@X1_array)
{
set_test_pst_Par(0,$X1_array[$i0]);
$setval0 = $X1_array[$i0];
$test_array[0] = $setval0;
$i0++;

$i1 = 0;
for (@X2_array)
{
set_test_pst_Par(1,$X2_array[$i1]);
$setval1 = $X2_array[$i1];
$test_array[1] = $setval1;
$i1++;

$i2 = 0;
for (@X2_array)
{
set_test_pst_Par(2,$X3_array[$i2]);
$setval2 = $X3_array[$i2];
$test_array[1] = $setval2;
$i2++;

$i3 = 0;
for (@Val_InpVar_array)
{
set_index($index_array[$i3]);
$setval3 = $index_array[$i3];
$i3++;

$getval = test_func ($setval3, $test_array[0], $test_array[1], $test_array[2]);

print ($getval);
}
}
}
}


#This is where the C code begins
__C__

// defines, includes...
short index;
short test_pst_Par;

uword CalcMap(uword index, const tMainMap * pst_Par)
{
some code
}

unsigned short test_func(short par0, short array0, short array1, short array2)
{
short array[3];
array[0] = array0;
array[1] = array1;
array[2] = array2;

return ((unsigned short)(CalcMap( par0, ((const tMainMap *)(long)(&array[0])))));
}

//This is where special functions are placed (get and set functions from C code to Perl code)

short get_test_pst_Par (short index) {return (*((short *)((long)(&test_pst_Par)+ index)));}

void set_test_pst_Par (short index, short value) {(*((short *)((long)(&test_pst_Par)+ index)))=value;}
void set_index (short value) {index=value;}


This does the trick but it took me some time to figure out this work around way ;o)

Anyway, I got it now but thanks a lot for trying to help out, I really appreaciate it.

______________
Dana
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top