0

Possible Duplicate:
What is the difference between char a[] = “string”; and char *p = “string”;

int main() {
 char *p="ayqm";
 char c;
 c=++*p;
 printf("%c",c);
 return 0;
}

Its output is a. See http://codepad.org/cbNOPuWt But I feel that the output should be b since c = ++*p. Anybody can explain the reason for the output?

Community
  • 1
  • 1
Mohammed H
  • 6,568
  • 15
  • 77
  • 122

1 Answers1

6

Sure, it's undefined behavior. Anything can happen.

You're attempting to modify a string literal, which is illegal.

If you do, for example

char c = *p;
++c;

you'll see the correct output.

The actual type of p should be const char*, in which case you'd get a compiler error.

Luchian Grigore
  • 245,575
  • 61
  • 446
  • 609