transfer the data between shared library and appli

2019-09-02 20:23发布

问题:

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 .

回答1:

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);