void main(void)
{
 char *p = "name";
 p[0] = 's';
 printf("%s", p); 
}

The above piece of code is giving me eror at run time. The reason for this error might be that I am not allocating any memory for the pointer(correct me if I am wrong).
My doubt is, In the first line of code what exactly is happening.

char *p = "name";

1) Will the compiler places the string "name" in the stack segment of the memory and then initializes p with the address where the string "name" is stored ?

2) Will the string "name" be in the stack???

3) If that is the case why I am getting the error at the run time. The memory is anyhow allocated to the string.

4) I commented out each line and tried debuging. That forced me to the conclusion that the error is in line number 2.
p[0] = 's'; What is the problem with this statement?

If I allocate the memory to the pointer using malloc the error vanishes. Why is it like that??

Recommended Answers

All 6 Replies

Assume string literals are nonmodifiable. Use a modifiable array.

char p[] = "name";

And main returns an int. Are you just starting out and have a bad reference?

If I use array i can modify the string, but y not in this case ????

I havent got answers to my questions ... if u can answer that also it would be a grt help

> The reason for this error might be that I am not allocating any memory for the pointer(correct me if I am wrong).
Yes, it has been allocated, but the memory which you're pointing at is read-only (meaning you can't change it). The variable p can be changed (say p="world"), but what it points at cannot be changed (you get the error).

> 1) Will the compiler places the string "name" in the stack segment of the memory
No, only p is on the stack, the string is somewhere else, allocated by the compiler.
In effect, the compiler does this for you. You just never see (or know) the name of the array it creates.

const char anon[] = "hello";
int main(void)
{
 char *p = anon;
 p[0] = 's';
 printf("%s", p); 
}

If you want something you can change, try

int main(void)
{
 char hello[] = "hello";
 char *p = hello;
 p[0] = 's';
 printf("%s", p); 
}

Both your pointer and string are on the stack, and you can change the string via your pointer.

> 2) Will the string "name" be in the stack???
No, see above.

> 3) If that is the case why I am getting the error at the run time. The memory is anyhow allocated to the string.
Because the 'anon' string itself is not WRITEABLE.
Yes, it is allocated, no you can't change it.

> void main(void)
main returns an int.

Your explanation says,

1) Will the compiler places the string "name" in the stack segment of the memory
No, only p is on the stack, the string is somewhere else, allocated by the compiler.
In effect, the compiler does this for you. You just never see (or know) the name of the array it creates.

What I understood is that the compiler places the string in some area which is not writable and assigns the starting address to p.
somewhere in the explanation is not clear.
Can you tell me where exactly compiler places that string?(which segment of memory)

Can you tell me where exactly compiler places that string?(which segment of memory)

This depends on the compiler, so you shouldn't worry about it. The standard has made this part implementation dependent. Infact it hasn't even said whether the same string literal used in two different places are equal or not.

For example

char* p = "string";
char* q = "string";

The above two string objects "string" can be stored in two different places or in the same location to save memory. The Microsoft Compiler enables both.

Microsoft Specific
In some cases, identical string literals can be "pooled" to save space in the executable file. In string-literal pooling, the compiler causes all references to a particular string literal to point to the same location in memory, instead of having each reference point to a separate instance of the string literal. /GF enables string pooling.
END Microsoft Specific

Reference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.