简体   繁体   English

左值作为一元'&'操作数

[英]lvalue required as unary ‘&’ operand

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? 我究竟做错了什么?

C preprocessor is at play here. C预处理器在这里发挥作用。 After the code is preprocessed, this how it looks like. 在对代码进行预处理之后,这就是它的样子。

char *ptr = (char *)&9987;

address of ( & ) operator can be applied to a variable and not a literal. & )运算符的地址可以应用于变量而不是文字。

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 ;这是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM