我有以下代码:我正在插入适当的评论。
#include <stdio.h>
int main(void)
{
char *x = "Hello"; //x points to a constant string
*x = 'B'; //Runtime error
char str[10] = "Hello"; //String is not constant
x = str; //x now points to a non-constant string
*x = 'B'; //Hence this is allowed.
}
因此,指针指向的值在运行时检查其常量,并相应地抛出错误。