[英]Define and array in C++
I have a question about this error from g++ on linux: 我对Linux上g ++的此错误有疑问:
srcs/../incs/file.hpp:21:27: error: taking address of temporary array
# define KEY_ESC_ (char[]){27, 0, 0, 0, 0, 0, 0}
^~~~~~~~~~~~~~~~~~~~~~
srcs/main.cpp:91:16: note: in expansion of macro 'KEY_ESC_'
This is in a define as you can see. 如您所见,这是在定义中。 I don't understand why g++ say taking address of temporary array
我不明白为什么g ++说要使用临时数组的地址
It's more global than temporary... 它比临时性更具全球性...
This value is the key escape got from read 此值是从读取获得的键转义
Any way ... 随便...
How can I solve it? 我该如何解决?
This code works on osx, but I need gross-compilation on linux ... 这段代码可在osx上运行,但是我需要在linux上进行粗编译。
Thank you 谢谢
如果使用define语句,则代码中的KEY_ESC_
所有实例将被(char[]) {27, 0, 0, 0, 0, 0, 0},
KEY_ESC_
逐字替换,这时代码中的临时变量。
I guess you have function like this: void f(char** A)
so you pass &KEY_ESC_
我猜你有这样的功能:
void f(char** A)
所以你通过&KEY_ESC_
{27, 0, 0, 0, 0, 0, 0}
is const char
array. {27, 0, 0, 0, 0, 0, 0}
是const char
数组。
by casting it to char[] you create a temporary variable. 通过将其强制转换为char [],可以创建一个临时变量。
the problem is that the method can save the address of the temporary and reuse it after it been released. 问题在于该方法可以保存临时地址,并在释放后重新使用。
You can try to solve it by use a const
: 您可以尝试使用
const
解决它:
const char KEY_ESC_[] = {27, 0, 0, 0, 0, 0, 0};
and call to method: 并调用方法:
void f2(const char** A)
with: 与:
f2(&KEY_ESC_);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.