Hello,

I would like to know hot to pass address reference to ‘c’ function through python. I am using swig for ‘c’ anf python interfacing

Interface file : stud.i
***************************************************

%module stud

%header%{
#include "stud.h"
%}

%include "stud.h"

%array_functions(char *,charp );

%inline %{
extern int parse1(char *,char *,char **);
%}

%inline %{
int print_args(char **argv) {
int i = 0;
for(i=0;i<=180;i++) {
printf("argv[%d] = %c\n", i,*(argv));
i++;
}
return i;
}
%}

**********************************************************

Stud.c
======================================
#include<string.h>
#include<stdio.h>
#include "stud.h"

int parse1(char *s, char *p,char **t)
{
strcat(p,s);
*t=&p[0];
}

**********************************************************
Stud.h
===================================

int parse1(char *,char *,char **);

**************************************************
>>> from stud import *
>>> a='rajashree'
>>> c='thoratf'
>>> d=new_charp(1000)
>>> parse1(a,c,d)
2300188
>>> d
<Swig Object of type 'char **' at 0x28aca0>


Now I want read value of d in python… How do I do it.
Any type of help is appreciated.

Did you try

print(charp_getitem(d, 20))

?

Thanks, tried, but it returns None.
>>> print(charp_getitem(c,20))
None
>>>

Thanks, tried, but it returns None.
>>> print(charp_getitem(c,20))
None
>>>

I don't understand the content of the function parse1. To use strcat, you must make sure that the destination char* is large enough to hold the result. I donc think that you can pass a python string like 'rajashree'.
Also it seems that d=new_charp(1000) creates an array of 1000 pointers to char, which is probably not what you want. See the documentation of %array_functions in swig.
So what do you want to do exactly ?

This was just a sample example. In real case, the functionality of parse1 function is quite different, instead of storing a plain string, we want to store binary data in memory pointed by char **

Like “'\xb5\x81\xca\x80\x01\x13\x83\x08Q\x10\x10\x00\x00\x91\x93\xff\xa4\x06\x80\x04\xc1\xc1\x00\x01\x85\x012\xa6\x06\x80\x04\xac\x10\x03\x0c\x87\x0bapn2.com.q2\x88\x02\xf1!\xa9\x08\xa0\x06\x80\x04++\x05H\x8b\x01\xff\xac604\xa1\x0e\x81\x0c\x01"r\rs\x96\xfe\xfe\x86\x07\xfe\xfe\xa2\x0e\x81\x0c\x01"r\rs\x96\xfe\xfe\x86\x07\xfe\xfe\x83\x01D\x84\x01`\x85\x01\x00\x86\t\t\x11\x05\x12\x07\x19-\x05\x00\x8d\t\t\x11\x05\x11Y3-\x05\x00\x8e\x02\x01\xd2\x8f\x01\x13\x91\x01\x01\x92\x071001acs\xb3!0\x1f\x06\x0b*\x86H\x86\xf6}\x04\n\x01\x01\x03\x81\x01\xff\xa2\r\x04\x0b!\x0f\xa0\x00\x00\x00D\x00\x00\x00`\x94\x012\x95\x01\x01\x96\x07\x91\x19\x00\x00\x00\x91\x93\x97\x02\x08\x00'”

If we simple return this in “char *” it will skip string after “\x00” as it consider null as a string end. So instead of sending string (char *) we would like to send address of this string buffer, so that we can read from that address.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.