You cannot just pass a pointer. Instead, you need to pass the address of the pointer. Try the following:
void test(char**); int main() { char *c=NULL ; test(&c); printf("After test string is %s\n",c); free(c);
The rationale is that the pointer is passed by value. This means that it is copied to the function. Then you overwrite the local copy inside the function with malloc.
So, to get around this, you need to pass the address of the pointer.
source share