【发布时间】:2014-02-03 05:29:14
【问题描述】:
我知道在 C 中我们可以通过指针修改“const int”。但是在编译程序时我在gcc中启用了'-O2'标志,而const int不能修改该值,所以只想知道gcc优化标志如何影响修改'const int'。
这是示例应用程序 test.c
#include <stdio.h>
#include <stdlib.h>
int main(int ac, char *av[])
{
const int i = 888;
printf("i is %d \n", i);
int *iPtr = &i;
*iPtr = 100;
printf("i is %d \n", i);
return 0;
}
gcc -Wall -g -o test test.c
./test
i is 888
i is 100
gcc -Wall -g -O2 -o test test.c
i is 888
i is 888
这种好奇心促使我写下这个问题。
【问题讨论】:
-
我收到警告。
initialization discards ‘const’ qualifier from pointer target type. -
“我知道在 C 中我们可以通过指针修改 'const int'”请问?