开发者

Strange Core Dump problem

开发者 https://www.devze.com 2023-01-20 01:52 出处:网络
My this code is dumping core : int main(int argc,char *argv[]) { char *p = \"onnm\"; printf(\"%c\\n\",++*(p++));

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

精彩评论

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