Hello to all progg. out there.
The sample prog. i have written is of which i am facing three doubts :

char *someFun1()
{
char temp[ ] = "string";
return temp;
}
char *someFun2()
{
char temp[ ] = {'s', 't','r','i','n','g'};
return temp;
}
int main()
{
puts(someFun1());
puts(someFun2());
}

I) Its gives the warning that The function returns the addr. of a local variable Can anyone explain the logic behind this?

II) replacing the char temp[] = {'s', 't', 'r', 'i', 'n', 'g'} with ptr char* temp = {'s', 't', 'r', 'i', 'n', 'g'} makes the prog jump out.

What is the prob i am facing here?

III) What is the difference between char temp[] = {'s', 't', 'r', 'i', 'n', 'g'} char temp[] = {"string"} Thanks for your help in advance.
Eagerly waiting for your reply.

I) Its gives the warning that The function returns the addr. of a local variable Can anyone explain the logic behind this?

You are declaring temp as a local array of characters. You are returning the address of this array. When the function exit, the memory for this local variable is lost, and it's contents cleared. So the address returned to you from the function is not of any use.

II) replacing the char temp[] = {'s', 't', 'r', 'i', 'n', 'g'} with ptr char* temp = {'s', 't', 'r', 'i', 'n', 'g'} makes the prog jump out.
What is the prob i am facing here?

You must mean char* temp = {'s', 't', 'r', 'i', 'n', 'g'} . Well temp is a pointer to a character. Not an array of characters.

]
III) What is the difference between char temp[] = {'s', 't', 'r', 'i', 'n', 'g'} char temp[] = {"string"}

The first one does not store the NULL terminator in temp. But the second one does.

This article has been dead for over six months. Start a new discussion instead.