changing the value of one of the pointer in case where, adrress of two pointers are same

StackOverflow https://stackoverflow.com/questions/19090868

  •  29-06-2022
  •  | 
  •  

Question

I have a problem,I saw that the address of two pointers are same as in question here(Addresses of two pointers are same), also answered by blue moon. Which lead me to some more doubts. Since both the pointers have same address I thought to change the value of one of the pointers, expecting value will be changed in other pointer too(since they have same address). But its giving segmentation fault. I am showing it in a code below.

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

int main()
{
    char * p = "abc";
    char * p1 = "abc";
    printf("%d\n %d\n", (void *)p, (void *)p1);
    printf("%s\n %s\n", p, p1);
    *p = 'b';
    printf("%d\n %d\n", p, p1);
    printf("%s\n %s\n", p, p1);
}
Was it helpful?

Solution

C90, 6.1.4

If the program attempts to modify a string literal of either form, the behaviour is undefined.

In your case this Undefined behaviour is in your favour so that you are getting Seg fault.Other than this printing pointer using %d is not a good practice, You should use %p.

Now from your given Link Answer of Blue Moon

you should always treat p and p1 as two different pointers (even though they have the same content) as they may or may not point to the same address. You shouldn't rely on compiler optimizations.

OTHER TIPS

The code

char * p = "abc";

Can put the string literal in read only memory so technically you shouldn't modify it. This could be giving you the seg fault. This is also why the two pointers may be the same... the compiler is clever enough to recognize they both use the same string literal so only stores one instance of the literal.

If you declared it as

char p[] = "abc"

Then this would create a readable/writable array of 4 chars (3 and 1 null terminator) on the stack, which you could then modify, However...

char p[] = "abc";
char p1[] = "abc";

You will find that p1 != p as both are created with separate storage on the stack (I'm assuming they're not globals)

Also when using printf to print pointer values I think it is better to use %p.

char * p = "abc";
*p = 'b';

invokes undefined behavior since you are trying to modify read-only memory. You can not change the memory, where this constant string literal resides.

Use this literal to initialize an array instead:

char myStr[] = "abc";
char *p = &myStr[0];
char *p2 = &myStr[0];  // <-- p and p2 point to the same address now
*p = 'b';
printf("%s\n", p2);    // <-- prints bbc

String literals are stored in read-only memory segments, so attempts to modify it cause segmentation faults. This is because string literals data can be referenced by many pointers and modifying it would modify other constant strings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top