Reputation: 832
I have shared library where I does some data generations/processing , and I have written some APIs and application to access them and transfer the data .
/**************APPLICATION*********/
char* data1;
char* data2;
genratedCamData(1, char* data1 , char *data2);
printf(" data1 %s ",data1);
printf(" data2 %s ",data2);
free(data2);
/************Inside Library ****************/
int genratedCamData(1, char* datafirst , char *datasecond)
{
if(CAM==1)
datafirst=getCam1data();
printf(" test at lib %s ",type);
datasecond=malloc(sizeof(char) * 100);
sprintf(datafirst,"%s",datasecond);
return 0;
}
I tried above method get the data to application , but data prints properly inside the library but out side the lib (in the application ) it does not print anything ...
Can some body help me to use a best way to communicate the data b/w libs and application .
Upvotes: 0
Views: 325
Reputation: 121961
As C passes arguments by value, including pointers, you need to pass the address of the pointers, char**
, to a function to enable the function to point the pointers to a different address and have those changes visible to the caller:
int genratedCamData(int CAM, char** datafirst , char **datasecond)
{
*datafirst=getCam1data();
*datasecond=malloc(100); /* sizeof(char) is guaranteed to be 1. */
}
Invoked:
char* data1;
char* data2;
genratedCamData(1, &data1 , &data2);
Upvotes: 4