【发布时间】:2021-09-17 14:32:14
【问题描述】:
struct test{
char c_arr[1];
};
test array[1] = {{1}};
test get(int index){
return array[index];
}
int main(){
char* a = get(0).c_arr;
return 0;
}
使用g++ 编译没有警告,但使用clang++ 会打印以下内容:
warning: temporary whose address is used as value of local variable 'a' will be destroyed at the end of the full-expression
这是不正确的吗? get(0).c_arr 不返回指向全局数组的指针吗?
还是 get(0) 返回一个临时变量,而编译器错误地认为 c_arr 只是它的一个实例,而不是全局变量?
编辑
为什么将这个临时变量传递给函数没有警告?
void call(char* in){}
int main(){
call(get(0).c_arr);
return 0;
}
【问题讨论】:
标签: c++ temporary-objects