Reputation: 445
I'm trying, using the Inline::CPP
module, to pass two pointers as arguments to a C function but I get the error No typemap for type int *. Skipping void swapPointer(int *, int *)
and I can't figure out what I have to do to achieve my goal.
I would like some help, please.
This is my code:
#!/usr/bin/perl
use Inline CPP;
my $x = 1 ;
my $y = 2 ;
swapPointer(\$x, \$y);
print "X:$x Y:$y" ; # expected: $x => 2 and $y => 1
__END__
__CPP__
void swapPointer(int* a, int* b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
Upvotes: 2
Views: 75
Reputation: 40778
You could try use SV *
instead of int *
, see this answer for more information. Her is an example:
Here is an example:
use feature qw(say);
use strict;
use warnings;
use Inline 'CPP';
my $x = 1 ;
my $y = 2 ;
swapPointer(\$x, \$y);
say "X:$x Y:$y" ; # expected: $x => 2 and $y => 1
__END__
__CPP__
void swapPointer(SV* rva, SV* rvb)
{
if (!SvROK(rva)) croak( "first input argument is not a reference" );
if (!SvROK(rvb)) croak( "second input argument is not a reference" );
SV *sva = (SV *)SvRV(rva);
SV *svb = (SV *)SvRV(rvb);
if (SvROK(sva)) croak( "first input argument is a reference to a reference" );
if (SvROK(svb)) croak( "second input argument is a reference to a reference" );
if ( SvTYPE(sva) >= SVt_PVAV ) croak( "first input param is not a scalar reference" );
if ( SvTYPE(svb) >= SVt_PVAV ) croak( "second input param is not a scalar reference" );
SV *temp = newSVsv(sva);
sv_setsv(sva, svb);
sv_setsv(svb, temp);
SvREFCNT_dec(temp); // Free the temporary SV
}
Output:
X:2 Y:1
Upvotes: 3