0

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 ?

Onkar Mahajan
  • 944
  • 2
  • 13
  • 16

2 Answers2

3

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

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

nos
  • 223,662
  • 58
  • 417
  • 506
1

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
  • 2,063
  • 1
  • 16
  • 24