Home: Perl Programming Help: Intermediate:
How to get values back from c function ?



ningji
User

Nov 11, 2012, 6:07 PM


Views: 5801
How to get values back from c function ?

so i have a c code, it has 2 APIs,
get_val(int &val1, int &val2);
set_val(int val1, int val2);

from perl, when i want to set new value,
`c_program $val1 $val2`;

to get value back from c program,
how to get 2 values back now ?

many thanks !


ningji
User

Nov 11, 2012, 6:47 PM


Views: 5799
Re: [ningji] How to get values back from c function ?

will this work ?

`c_program get_val \$var1 \$var2`

In Reply To


Laurent_R
Veteran / Moderator

Nov 11, 2012, 11:13 PM


Views: 5793
Re: [ningji] How to get values back from c function ?

Difficult for me to say, we don'ty know anything about your functions, but your first function expects a pointer to the int values. Passing a reference to your variables seems to be the right way to do so (and that's what I am doing quite regularly when calling relatively similar libraries). As for the second function, it expects values, so you would need to pass directly your variables.

I am a but surprised, though, that your C functions don't return any values (not even an error code).


ningji
User

Nov 12, 2012, 4:01 PM


Views: 5766
Re: [Laurent_R] How to get values back from c function ?

actually i found another way around it, thanks !


Laurent_R
Veteran / Moderator

Nov 13, 2012, 10:28 AM


Views: 5759
Re: [ningji] How to get values back from c function ?

And can you telle what this other way is, for the benefit of the communauty?


ningji
User

Nov 13, 2012, 11:53 AM


Views: 5757
Re: [Laurent_R] How to get values back from c function ?

sure, actually it has nothing to do with perl.

my c code is like this
void get_func(int *v1, int *v2, int *v3);

instead of playing with call by reference,
i just added 1 line in get_func,
cout << *v1 << " " << *v2 << " "<< *v3<<endl;

in perl i can just split the out into 3 ints.



In Reply To
And can you telle what this other way is, for the benefit of the communauty?