Why does this small C program crash?

后端 未结 4 433
有刺的猬
有刺的猬 2021-01-28 12:33

The program is:

#include 
#include 
int main(void) {
    char *a=\"abc\",*ptr;
    ptr=a;
    ptr++;
    *ptr=\'k\';
    printf(\"         


        
4条回答
  •  一向
    一向 (楼主)
    2021-01-28 13:09

    This:

    char *a="abc";
    

    is really:

    const char *a="abc";
    

    You can't modify ptr, which points to the same address as a.

提交回复
热议问题