Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下代码行:
#define PORT 9987
和
char *ptr = (char *)&PORT;
这似乎适用于我的服务器代码。但是当我在客户端代码中编写它时,它会给出以下错误消息:
lvalue required as unary ‘&’ operand
我究竟做错了什么?
C 预处理器在这里发挥作用。代码预处理后,它的样子。
char *ptr = (char *)&9987;
( &) 运算符的地址可以应用于变量而不是文字。
&
预处理器宏没有内存,并且在编译时宏被替换为值。所以实际上这里发生的事情是char *ptr = (char *)&9987;,这是不可能的。
char *ptr = (char *)&9987