Let's start with a warning. You stated:
char* cPTest[]
in English: " cPTest is an array of pointers to char"
and
char* makePointerCopy(char cIn[]);
in English: " makePointerCopy() takes an array of characters and returns a pointer to char"
So you are trying to assign a "pointer to char" to an "array of pointers to characters". Do you see the problem? I would suggest carefully checking types before completing assignments.
This means that you really want to declare makePointerCopy() to return a "pointer to a pointer to a char":
char **makePointerCopy(char cIn[]);
because at the end you will return a pointer to the first element of the returned array.
Another important point: you declared "cOut" a local variable for the function.
char* makePointerCopy(char cIn[]) { char* cOut[sizeof(cIn)/sizeof(cIn[0])]; ... return cOut;
Remember that local variables automatically become invalid after the function finishes. To βsaveβ it, you can declare it static :
char* makePointerCopy(char cIn[]) { static char* cOut[sizeof(cIn)/sizeof(cIn[0])]; ... return cOut;
Note that you must be well disciplined when returning this type of value.
Alternatively, you can allocate the space you need with malloc() until you remember free() it when you no longer need it.
source share