Why does this small C program crash?

后端 未结 4 489
有刺的猬
有刺的猬 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:08

    The reason is that your string "abc" lives in a read-only area of memory. It gets put there by the linker. You try to change it in your program, and all bets are off.

提交回复
热议问题