-1

char * const pstr = "abcd"; pstr is a const pointer to char... I think that I can't modify the pstr, but I can modify *pstr, So I write the next code

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    //  The pointer itself is a constant,
    //  Point to cannot be modified,
    //  But point to a string can be modified
    char * const pstr = "abcd";  //  Pointer to a constant

    // I find that pstr(the address of "abcd") is in ReadOnly data
    // &pstr(the address of pstr) is in stack segment
    printf("%p  %p\n", pstr, &pstr);

    *(pstr + 2) = 'e';  //  segmentation fault (core dumped)
    printf("%c\n", *(pstr + 2));

    return EXIT_SUCCESS;
}

But the result is not as I expected. I got a segmentation fault (core dumped) at the line 14... So I write the next code

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


int main(void)
{
    //  The pointer itself is a constant,
    //  Point to cannot be modified,
    //  But point to a string can be modified
    char * const pstr = "abcd";  //  Pointer to a constant

   // I find that pstr(the address of "abcd") is in ReadOnly data
   // &pstr(the address of pstr) is in Stack segment
   printf("%p  %p\n", pstr, &pstr);

   *(pstr + 2) = 'e';  //  segmentation fault (core dumped)
   printf("%c\n", *(pstr + 2));

   return EXIT_SUCCESS;

}

But I don't know why???

gatieme
  • 105
  • 9
  • 1
    You are trying to modify a string literal. See [this](http://stackoverflow.com/questions/10202013/change-string-literal-in-c-through-pointer). – SSWilks Oct 04 '15 at 13:02
  • 4
    Possible duplicate of [Why do I get a segmentation fault when writing to a string initialized with "char \*s" but not "char s\[\]"?](http://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-string-initialized-with-cha) – SSWilks Oct 04 '15 at 13:03

4 Answers4

2
char * const pstr = "abcd"; 

pstr is a constant pointer to char and you can't modify pstr correct , but "abcd" is a string iteral . And you can't modify string literal .

You try to modify it and therefore , you get a segmentation fault .

ameyCU
  • 16,489
  • 2
  • 26
  • 41
0

In C language , If i will write

char *ptr = "abcd"

you can not change *(ptr+2) = 'e' , even if you don't write const. as in C language if you declare like char *ptr = "abcd" , it will treat ptr as constant character string.

so it will give segmentation fault in this code also ..

char *ptr = "abcd";
*(ptr+2) = 'e';

but , you can do this ::

char ptr[] = "abcd";
*(ptr+2) = 'e';
KrunalParmar
  • 1,062
  • 2
  • 18
  • 31
0

char * const pstr = "abcd"

This is pointer initialization. so "abcd" is stored in code section of Memory. you can not modified code section of Memory. it is read only memory. so. OS will generate segmentation fault during run time because of accessing unauthorised memory.

char pstr[]="abcd";

now, "abcd" stored in data section so u can modified.

0

the difference b/w string initialised by array and string initialised by pointer is that pointer string cannot be modfified .The reason is that pointer string is stored in code memory and array string is stored in stack(or heap if it is global array)

kapil
  • 625
  • 12
  • 20