I’m new to C and I have created the following code, but when run the program crashes. Why? How do I stop it crashing?
char *get()
{
char *n;
printf("\n user name : ");
scanf("%s", &n);
return n;
}
int main()
{
char *c = get();
printf("%s", c);
return 0;
}
In C it is typical for the parent to handle memory allocation since there’s no garbage collector to clean up after yourself, so it has to do that anyway:
http://ideone.com/Tw347