0

I was trying to understand segmentation fault for char * assignment during for the following program for at location *p = 'Z'

void main()
{
   char *p ="abcdefg";
   *p = 'Z';
}

When Googled, i did find many links to answers as follows 1. This is a string literal and once assigned cannot have its value changed 2. String literals cannot be assigned to *p 3. Take a malloc which can be changd... and so on...

But my worry was if string literals values can't be changed as it is constant, how come we don't segmentation fault when we change constant value of a integer. Can someone please help me understand this better?

-Prashanth

pkumarn
  • 1,383
  • 4
  • 22
  • 29
  • 1
    Have a look at : http://stackoverflow.com/questions/718477/string-literals – loxxy Aug 03 '13 at 12:14
  • I assume you have heard that string literals are often called string constants. Which part of the word "constant" don't you understand? –  Aug 03 '13 at 12:22
  • Does the same string literal concept apply to integers ? #include void main() { int *a = 123456; *a = 1; } – pkumarn Aug 03 '13 at 16:27

4 Answers4

2

It's undefined behaviour. If you have a constant string, the Standard says that string literals may not be modified. So wether it is writable or not depends on the architecture, and it may, or may not segfault.

update added from comment.

Devolus
  • 21,661
  • 13
  • 66
  • 113
  • "it's put in the data segment" also depends on the architecture, don't make assumptions. "It's undefined behavior" is in itself enough; probably append "because the Standard says that string literals may not be modified" for an even better answer. –  Aug 03 '13 at 12:24
0
char *p ="abcdefg";// p is pointing a read only memory.
   *p = 'Z';       //You want write a read memoty . 
Lidong Guo
  • 2,817
  • 2
  • 19
  • 31
  • Wrong. "abcdefg" is stored somewhere in memory. `*p` is a pointer to that address. – ep0 Aug 03 '13 at 12:18
  • 2
    @ep0 No, it's your explanation that is wrong. The `*` operator dereferences the pointer. –  Aug 03 '13 at 12:19
0

the first * is variable for point, it means p is a point, which points to a char,so int *p is a point which points to a integer value, this is variable for point. p,not *p, is a point, its value is address of char value in memory, you can test it by printf("p=%d",p); p contains value of *p, or a char variable. so, you can say *="value pointed by pointer...", and & ="address of value...in memory". so, instead of char *p, temp; temp='A'; *p=temp;(value that the pointer p points equal value of variable temp)

-1

It's because you're using * for assignment. Your code basically reads: At the address of p which is of type pointer store value Z, which is of type char.

ep0
  • 710
  • 5
  • 13
  • 1
    "At the address of p which is of type pointer" - except that it isn't, `*p` dereferences the pointer p, so the assignment tries to change the value of the first character, which should not be modified because it's part of a string literal. –  Aug 03 '13 at 12:20