Originally Posted by
laserlight
Have a pointer in the main function to store the return value of foo, then use free on it after printing.
thanks i made this
Code:
#include <stdio.h>
#include <string.h>
char *foo(char *a, int n){
char *d,*c;
int i;
d = (char *)malloc(n*sizeof(char));
c=d;
n=strlen(a)-n;
for (i=n;i<strlen(a);i++){
*(d+i-n) = *(a+i);
}
*(d+i-n) = '\0';
return c;
}
int main(void){
char b[]="test";
char *res;
res = foo(b,3);
printf("%s\n",res);
free(res);
printf("%s\n",res);
getchar();
return 0;
}
why does it print the same thing after using free()
i thought it is supposed to print nothing since we free the allocated memory
thanks