Function that returs char pointer behaives strange!!
Hi folks!!!
I have a noticied a 'strange' thing when a change mine function-definition a little and I canīt understand why it doesnīt work.
I have a funtion like
Code:
char * test()
{
char *name = "Test";
return name;
}
and this works perfectly well. But when I change it to
Code:
char * test()
{
char name[5];
name[0] = 'T';
name[1] = 'e';
name[2] = 's';
name[3] = 't';
name[4] = '\0';
return name;
}
I get a warning message that "returning address of local variable or temporary". I think it has to to something that the array loses it scope or something but why is the first code-snippet working!!!!
(Second code will give an output that is wrong).
And also is a function that returns a pointer the same thing as a function that returns an array??
Iīve been searching for a post that 'returns an array' but didnīt found one that solves mine problem.