Hello,
I have a short example of a C library that I have coded below along with the header. I am calling this function from a C++ client app.
Some of the warning I get are as follows. This normally gives me a segmentation stack run time error. So I guess some about memory not been allocated being assigned to null address.
warning: return from incompatible pointer type
warning: function returns address of local variable
I have tried many different ways to do this. As you can see from my commenting out pieces of code.
However, I can return pointer without any problems. However, my code needs to return a string array. i.e. char msgTest[] = "Hello how are you";
Can any one offer any advice on this?
Many thanks for any suggestions.
And the C++ code for calling the above function.Code://C library code //Header cHeader.h #ifdef __cplusplus extern "C" { #endif char* returnMessage(char *message); #ifdef __cplusplus } #endif //Implementation cDriver.c char* returnMessage(char *message) { printf("Message: %s\n", message); char *msg = NULL; // //msg = message; // printf("msg = message: %s\n", msg); // //strcpy(msg, message); // printf("strcpy(msg, message): %s\n", msg); char msgTest[] = "Hello how are you"; strcpy(msg, msgTest); return &msgTest; }
Code:int main(int argc, char** argv) { char *ptrTest = NULL; char test[] = "Hello"; ptrTest = test; char *ptrRtn = NULL; ptrRtn = returnMessage(ptrTest); cout << "PtrRtn: " << ptrRtn << endl; return 0; }


