-5

I have trouble understanding the variations among these :

char* s = "string";

cout<<*s+1;
cout<<(*s)++;

cout<<*s++;
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
kitkat
  • 99
  • 5
  • Have tried, say uhhhh, *running it?* – Fantastic Mr Fox Sep 18 '14 at 03:41
  • I get a segmentation fault. Probably because you are modifying a `const char *`. The first line should read: `const char * s = "string";`, and then you cannot do `*s++`. Modifying a const value is undefined behavior: http://stackoverflow.com/questions/19372952/modifying-a-char-const-string – tillaert Sep 18 '14 at 03:47

2 Answers2

1

simply speaking, “string” located in ROM area, how can you modify them ?

thomas
  • 505
  • 3
  • 12
0

*s+1 statement adds 1 with the ASCII value of the 1st element of the string and prints it.
And *s prints the 1st element of the string.

MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
  • ASCII, probably not. Should say "adds 1 with the code unit value". Even if we assume, in the actual character set encoding, all codepoints are one code unit, incrementing a codepoint doesn't necessarily produce a valid codepoint. For instance, [Windows-1252](http://en.wikipedia.org/wiki/Windows-1252) has holes. – Tom Blodget Sep 18 '14 at 17:02