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