C语言 左值需要作为一元“&”操作数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16727370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
lvalue required as unary ‘&’ operand
提问by Indradhanush Gupta
I have the following lines of code :
我有以下几行代码:
#define PORT 9987
and
和
char *ptr = (char *)&PORT;
This seems to work in my server code. But as I wrote it in my client code, it gives this error message :
这似乎适用于我的服务器代码。但是当我在客户端代码中编写它时,它给出了以下错误消息:
lvalue required as unary ‘&' operand
What am I doing wrong?
我究竟做错了什么?
回答by Lazylabs
C preprocessor is at play here. After the code is preprocessed, this how it looks like.
C 预处理器在这里发挥作用。代码预处理后,这是它的样子。
char *ptr = (char *)&9987;
address of (&) operator can be applied to a variable and not a literal.
( &) 运算符的地址可以应用于变量而不是文字。
回答by akhil
The preprocessor macros have no memory and at compile time the macro is replaced with the value. So actualy thing happening here is char *ptr = (char *)&9987;, which is not possible.
预处理器宏没有内存,在编译时宏被值替换。所以这里发生的实际事情是char *ptr = (char *)&9987;,这是不可能的。

