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??

Dani AI

Generated

Short answer: char *p = "name"; makes p a local pointer (stored on the stack) that points at a string literal with static storage duration. The literal itself is not a stack object and attempting to change its characters (for example p[0] = 's') is undefined behavior — that is why the program can crash at runtime. This ties back to ’s and ’s points: an array copy is writable, a pointer to a literal is not reliably writable.

Why this happens in practice: the C standard gives string literals static storage duration and does not require them to be writable. Compilers commonly place them in a read-only data segment (and may merge identical literals to save space), so writes to them can fault or produce surprising results — an implementation detail noted by . Because the behavior is undefined, different compilers/OSes can react differently.

Safe alternatives (implementation-neutral):

  • Use a modifiable array initialized from the literal so the writable copy has automatic storage duration (this is what suggested).
  • Allocate writable memory on the heap, copy the literal into it, then modify.

Example (heap copy):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    const char *lit = "name";
    char *p = malloc(strlen(lit) + 1);
    if (!p) return 1;
    strcpy(p, lit);
    p[0] = 's';     /* safe: p points to heap memory */
    printf("%s\n", p);
    free(p);
    return 0;
}

Practical notes: declare nonmodifying pointers as const char * to document intent; turn on compiler warnings and sanitizers to catch undefined behavior; prefer int main(void) and always free heap memory. These points expand on the earlier replies and explain why allocating (or using an array) resolves the runtime error reported by .

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.

Thanks guys,

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.