tags:

views:

47

answers:

3

My this code is dumping core :

int main(int argc,char *argv[])
{
   char *p = "onnm";
   printf("%c\n",++*(p++));
   return 0;
}

What might be the reason in printf line ?

+3  A: 

string literals are read-only, you can't change them.

Use e.g. char p[] = "onnm";

nos
+1  A: 

See this question.

BlueRaja - Danny Pflughoeft
+1  A: 

You are able to code like this because of the "an inconsistency in the language standard" of C. e.g.,

const char const_buff[] = { 'o','n', 'n', 'm', '\0' }; // OK

char* pArray = const_buff;            // not OK

In the same line it should have not allowed you to compile,

char *p = "onnm";

But you are able to compile, so allowing you to do the the mistake of changing a read-only string.

++ * ( p++)
yadab