2

Possible Duplicate:
getting segmentation fault in a small c program

Here's my code:

char *const p1 = "john";

p1[2] = 'z';  //crashes

printf("%s\n", p1);

I know p1 is a "read-only" variable, but I thought I could still modify the string ("john" ). I appreciate any tips or advice.

Community
  • 1
  • 1
Caffeinated
  • 11,982
  • 40
  • 122
  • 216

7 Answers7

4

You cannot safely modify string literals, even if the pointer doesn't look const. They will often be allocated in read-only memory, hence your crashes - and when they're not in read-only memory modifying them can have unexpected consequences.

If you copy to an array this should work:

char tmp[] = "john";
char *const p1 = tmp;
p1[2] = 'z'; // ok
bdonlan
  • 224,562
  • 31
  • 268
  • 324
3

Keyword const means that variable of that type should stay constant. You shouldn't change it.

Also even if you declare this string as char *p1 = "john"; it would be constant string literal and changing it would cause undefined behaviour. You should declare it as char p1[] = "john"; in order to achieve behaviour you are looking for.

LihO
  • 41,190
  • 11
  • 99
  • 167
3

1) Forget the "const" qualifier. This is WRONG, in C and C++, on ANY platform:

  char *p1 = "john";
  p1[2] = 'z';  //crashes

Here's why:

2) So how do you get around the access violation?

Simple: you allocate writable memory (instead of writing to a string constant, which is probably allocated in read-only memory):

  #define BUFSIZE 80
  ...
  char p1[BUFSIZE];
  strcpy (p1, "john");
  p1[2] = 'z';  //no problem

3) OK: so then what's the deal with "const"?

You were right about that part. Here's (one of many) discussions about the (subtle) difference between a "const pointer" and a "pointer to a const":

http://www.codeguru.com/cpp/cpp/cpp_mfc/general/article.php/c6967

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
2

your pointer points to memory that isn't allowed to be changed ( the constant string )

do char p1[100] = "john";

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
1

This is because you are not supposed to modify constants: after all, they are called constants for a reason. Modifying a constant is undefined behavior according to the C standard, which often means that your program is going to crash.

Note that it has nothing to do with your pointer being constant: the crash is because what your pointer points to is a string constant.

Here is how to do what you are trying to do legally:

char p1[] = "john";
p1[2] = 'z';  //no longer crashes :)
printf("%s\n", p1);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

There are two, unrelated, problems in that code. First, the const is not in the right place.

char * const p = "john";
char const * p = "john";
const char * p = "john";

The latter two are pointers to unmodifiable strings. If you had done this, then the code would not have compiled.

The first option char * const is not really a read-only variable. It means a pointer which points to modifiable data, and that the pointer cannot be changed such that it points to another string. But that's not relevant to your problem.

Your constis not relevant here. You have attempted to modify a string that shouldn't be modified. Literal strings should never be modified, and this is the cause of your crash.

Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
0

p1 is a pointer to constant data, not a constant pointer. Moreover it points to a literal constant, which typically resides in the code space, and modern operating systems and processors usually protect against code trying to modify code space.

You should not be surprised that it crashes, but in general the behaviour is undefined.

Clifford
  • 88,407
  • 13
  • 85
  • 165