开发者

Strange Core Dump problem

My this code is dumping core :

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

What mi开发者_开发知识库ght be the reason in printf line ?


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

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


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++)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜