-1

I have a following program:

#include<stdio.h>

char * test()
{
char * rt="hello";
return rt;

}

void main()
{
printf("\n %s \n", test());
}

here, it correctly prints hello while if rt is not a constant pointer like char rt[]="hello" it prints garbage. My understanding, in latter stack gets freed when function returns from test but what happens with above case? where does the memory for char *rt is allocated?

Extending above part, If I try to do char rt[]="hello" and if i try rt="hrer" it throws error while with char *rt="hello" it works fine but we can not change particular character in a string with later case. Please help me to understand it. Thanks.

debonair
  • 2,505
  • 4
  • 33
  • 73
  • 1
    can someone help me understand why the downvotes on the OP question? – John Jesus Oct 15 '13 at 20:38
  • @JohnJesus; Because OP had not searched SO correctly before asking this question, there is already a question with 4 answers. And also title of question is far from the question. – haccks Oct 15 '13 at 20:41
  • 2
    Whatever book or tutorial told you to use `void main()`, find a better one. `int main(void)` is correct. – Keith Thompson Oct 15 '13 at 20:42
  • @KeithThompson: Could you please explain why int main is correct and former is not correct ? – debonair Oct 15 '13 at 20:43
  • 2
    @RoshanC.: Because the C standard says so. `main` can be defined as `int main(void)` or as `int main(int argc, char *argv[])` or equivalent. Other *implementation-defined* forms may be permitted, but there's rarely any reason to use anything other than those two. (It's a different story for freestanding implementations, basically embedded systems.) The [comp.lang.c FAQ](http://www.c-faq.com/) is an excellent resource; see questions 11.12a and 11.12b. – Keith Thompson Oct 15 '13 at 20:52
  • @KeithThompson Thanks for the link. you got my upvote :) – debonair Oct 15 '13 at 21:18

2 Answers2

2

Your string "hello" is what is called a string literal. It resides in what is called the data segment of your program, which is a region of memory. Any other string literals throughout your code are put there as well. This region is loaded once, and never destroyed.

So, your pointer rt is pointing somewhere into that region.

But, if you declare char rt[] = "hello", you are declaring an array named rt[] on the stack and the array is 6 bytes long (hello + null terminator). When the function returns, the stack is freed, so, this memory will be invalid.

Some more information on string literals are here: C String literals: Where do they go?

Community
  • 1
  • 1
John Jesus
  • 2,284
  • 16
  • 18
  • 1
    For Christ sake why the screen name John Jesus? – Ed Heal Oct 15 '13 at 20:42
  • @EdHeal; Ya. Good question. – haccks Oct 15 '13 at 20:43
  • @edheal hehe, that's my real name. – John Jesus Oct 15 '13 at 20:49
  • @JohnJesus - Are you a Muslim? Surely with the surname Jesus you are Jewish – Ed Heal Oct 15 '13 at 20:55
  • @EdHeal; Why do you think moslims uses `jesus` as surname? – haccks Oct 15 '13 at 20:57
  • @haccks - Tongue in check humour. Anyway Jesus was a Jew and they do not seem to like Muslims – Ed Heal Oct 15 '13 at 20:59
  • @haccks - There was no Christianity before Jesus. He was born into the Jewish faith. Guess he was a crafty Jew that wanted a slice of the action and made up another religion from all the others kicking around the Mediterranean at the time (see . Yes I am an Atheist and I think religion is just a con to control people. – Ed Heal Oct 15 '13 at 21:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39304/discussion-between-haccks-and-ed-heal) – haccks Oct 15 '13 at 21:15
1

The string Hello gets set into the read portion of the executable part of the program. The function returns a pointer to that.

The use of an array (in the second case) means that it gets copied onto the stack.

End of the function it gets zapped - hence garbage

Ed Heal
  • 59,252
  • 17
  • 87
  • 127