>>because when exiting f1 the memory is freed and the pointer points to nirvana. right?
It won't work, but not how you think. The problem is that mystring is a pointer to garbage and you try to use fgets to read into memory you don't have. This should cause an access violation of some sort. What you need to do is allocate memory first.
Code:
#include <stdio.h>
#include <stdlib.h>
char *f1(void)
{
char *mystring;
if ((mystring = malloc(100)) == 0)
{
return 0;
}
fgets(mystring,100,stdin);
return mystring;
}
void f2(char *mystring)
{
printf("%s\n", mystring);
}
int main(void)
{
char *mystring = f1();
if (mystring != 0)
{
f2(mystring);
}
return 0;
}