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?
If you are getting "lvalue required" you have an expression that produces an rvalue when an lvalue is required. For example, a constant is an rvalue but not an lvalue. So: 1 = 2; // Not well formed, assigning to an rvalue int i; (i + 1) = 2; // Not well formed, assigning to an rvalue.
This error occurs when we put constants on left hand side of = operator and variables on right hand side of it. Example: #include <stdio.h> void main()
Best practice to avoid such mistakes in if conditions is to use constant value on left side of comparison, so even if you use "=" instead "==", constant being not lvalue will immediately give error and avoid accidental value assignment and causing false positive if condition.
Which Arduino has a pin 60? In the C programming language, a lvalue is something you can assign a value to and an rvalue is a value that can be assigned. The names just mean 'left side of the equals' and 'right side of the equals'.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With