Say, among other elements I want to parse a C structure, from file1.c :
typedef struct mynode{
int* x;
int length;
}node;
int callerFunction(int myLength){
//memory space
node* n = (node*)malloc(sizeof(node));
//dummy values for explanation purpose only
double d = 3.14;
int max = 100;
//populate struct
n->length = myLength;
for(int i =0; i < n->length; i++)
n->x[i]= i;
//calling foo passing an structure
int result = foo(3,d,max,n);
}
and I want to pass this structure to another function via va_arg, in file2.c
int foo(int n,...){
int foo_max;
double foo_d;
node* foo_n;
va_list ap;
va_start(ap,n);
d = va_arg(ap,double);
foo_d = va_arg(ap,int);
foo_n = va_arg(ap,node*);
va_end(ap);
....
}
I thought I was doing the right thing, however if I include the structure, the data collected by foo is totally wrong (not the right data). What am I doing wrong here?
Inside
callerFunctionseems like you have not allocated memory for
n->xbut using it.