我正在研究 C 中的只读复合文字。当我尝试在取消引用运算符的帮助下更改其值时,值发生了变化!我现在很困惑为什么会这样。
此外,当我编译并运行程序(不尝试更改其值)时,它显示此错误:
Code_Testing.c:5:14: warning: initialization discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
5 | int *r = (const int[3]){6, 14, -98}; // Read Only compound Literal
|
我不明白为什么它忽略了const
限定符。
我知道const
限定符会丢弃任何更改,但这里复合文字的值发生了变化!
你能解释一下我在哪里犯了错误吗?
我写的程序是这样的:
#include <stdio.h>
int main(void)
{
int *r = (const int[3]){6, 14, -98}; // Read Only compound Literal
*r += 99;
printf("The changed value is = %d", *r);
return 0;
}
输出是:
Code_Testing.c:5:14: warning: initialization discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
5 | int *r = (const int[3]){6, 14, -98}; // Read Only compound Literal
| ^
The changed value is = 105