I can't understand why the whole thing doesn't work.
I just want to do malloc in the function func, when I return from it, the malloc disappears... and I get
* glibc detected ./test: free(): invalid pointer: 0xb76ffff4 **
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
int func(char *p) {
p=(char*)malloc(1);
*p='a';
printf("1. p= %c\n",*p);
return 0;
}
int main()
{
char *p;
func(p);
printf("2. p= %c\n",*p);
free(p);
return 0;
}
pis only a copy of the pointer.