Imagine this code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSTRSIZE 2048
int main()
{
char *str;
str = get_string();
return 0;
}
char * get_string()
{
char temp[MAXSTRSIZE], *str;
fgets(temp,MAXSTRSIZE,stdin);
str = malloc( sizeof(char) * (strlen(temp) + 1) );
strcpy(str, temp);
return str;
}
Do I need to free() the temp
variable in function get_string
?
What if did the get_string
code inside the main()
?