You can return the address of allocated space OR you can pass an address as an argument and have it "serviced".
Either way , the allocated space MUST be free()'d when the program is done with it.
If it is not it then becomes a "memory leak".
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * return_str( size_t len)
{
char * s = calloc(1, len);
strncpy(s, "hello", len);
return s;
}
void assign_str(char * t, int len)
{
char * s = calloc(1, len);
strncpy(s, "hello2", len);
t = s;
}
int main(void)
{
char * sp1;
sp1 = return_str(32);
printf("sp1: %s \n", sp1);
free(sp1);
assign_str(sp1, 32);
printf("sp1: %s \n", sp1);
free(sp1);
return 0;
}
At least I think that's right...