简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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