【发布时间】:2019-06-17 09:32:44
【问题描述】:
假设我们有一个引用为const int* 的int 变量,而该变量又别名为int *。如果通过int * 指针修改变量是否是未定义的行为,标准是否清楚?
作为说明,请考虑以下代码:
void increment(int* p) {
(*p)++;
}
void call_increment(const int* p) {
increment(p);
}
int main(void) {
int x = 7;
int* p = &x;
call_increment(p);
}
【问题讨论】:
-
您显示的代码格式错误,资格转换无效
-
GCC 的默认值:
warning: passing argument 1 of ‘increment’ discards ‘const’ qualifier from pointer target type
标签: c pointers constants language-lawyer undefined-behavior