【发布时间】:2021-11-08 05:05:36
【问题描述】:
我正在学习 C 语言中的指针。 当我声明:
char c1[] ="hello";
c1[0] = c1[1];
printf("%s\n", c1);
它打印出eello
但是当我执行以下操作时:
char * c2="hello";
c2[0] = c2[1];
printf("%s\n", c2);
它在 C# 中编译,但程序崩溃。 你能帮我弄清楚当我执行程序时堆栈中发生了什么吗?
【问题讨论】:
-
char *c2 = "hello"是一个字符串文字。修改它会导致UB。
标签: arrays c pointers nspointerarray