是否在运行时决定指向的字符串是否为常量?

时间:2014-07-09 20:38:09

标签: c

我有以下代码:我正在插入适当的评论。

#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.
}

因此,指针指向的值在运行时检查其常量,并相应地抛出错误。

0 个答案:

没有答案