C error: lvalue required as unary '&' operand - c

I have a code error but not sure what's wrong with my casting and reference.
BOOL xMBPortSerialPutByte( CHAR ucByte )
{
CDC_Send_DATA(&((unsigned char)ucByte), 1); // code error here
xMBPortEventPost(EV_FRAME_SENT);
return TRUE;
}
The CDC_Send_DATA is defined as the following:
uint32_t CDC_Send_DATA (uint8_t *ptrBuffer, uint8_t Send_length);
Here is the error message:
port/portserial.c:139:19: error: lvalue required as unary '&' operand
Hope someone could help. Thanks!

The cast operation causes a conversion, yielding an rvalue. An rvalue doesn't have an address, so you can't operate on it with a unary &. You need to take the address and then cast that:
CDC_Send_DATA((unsigned char *)&ucByte, 1);
But to be most correct, you should probably match the argument type in the cast:
CDC_Send_DATA((uint8_t *)&ucByte, 1);
Checking the return value would probably be a good idea too.

Related

can you explain why compile time error in the below code

why im getting errors in the code given below....
#include <stdio.h>
void foo(int*);
int main()
{
int i = 10;
foo((&i)++);(in this line error shows like this) //error: lvalue required as increment operand
}
void foo(int *p)
{
printf("%d\n", *p);
}
From Member access operators:
The address-of operator produces the non-lvalue address of its operand, suitable for initializing a pointer to the type of the operand.
And from Increment/decrement operators:
The operand expr of both prefix and postfix increment or decrement must be a modifiable lvalue of integer type (including _Bool and enums), real floating type, or a pointer type.
Simply put the & operator does not produce an appropriate object for the ++ operator.

error: lvalue required as unary ‘&’ operand, I've seen solutions but I can't relate them to my query

I can easily remove this error by replacing&(name+0) by &name[0] or just name, but why I'm getting this error??
#include<stdio.h>
#include<string.h>
void print(char *c)
{
while(*c != '\0')
{
printf("%c",*c);
c++;
}
printf("\n");
}
int main(void)
{
char name[]="Hello World!";
print(&(name+0)); //ERROR here
return 0;
}
Use of
print(&(name+0)); //ERROR here
seems to be a result of some misunderstanding.
First things first - why is it a syntactic error?
(name + 0) does not evaluate to an lvalue. Hence, you cannot take the address of the value of that expression.
A simple way to decide whether something is an lvalue or not is to ask yourself: Can I use it on the LHS of an assignment operator? In your case, you have to ask: Can I use
(name + 0) = <something>;
The answer is "no".
If you want to pass the address of the first element of name to print, you can use couple of methods.
print(&(name[0])); // Explicitly get the first element's address.
print(name); // The array decays to the address of the first element.
To quote the C11 standard in section 6.3.2.1 subsection 1:
An lvalue is an expression (with an object type other than void) that
potentially designates an object; if an lvalue does not designate an
object when it is evaluated, the behavior is undefined. When an object
is said to have a particular type, the type is specified by the lvalue
used to designate the object.
In this case, you've got the expression (name + 0). While name itself designates an object and is therefore an lvalue, the result of the addition in that expression does not designate an object, but rather a value, and is not an lvalue and thus ineligible for the unary & operator.
The &(name+0) is not equivalent to &name[0]. &name[0] is the same as &(*(name + 0)). Note the indirection, when you are using the subscript operator it's the same as offsetting the pointer and dereferencing it.

Why It throws an error lvalue is required?

#include<stdio.h>
int main(){
int i=0,j=1;
printf("%d",++(i+j));
return 0;
}
In this code I have used the increment operator but i don't know why it throws an error.
It throws the error:
lvalue is required.
You are trying to increment an integer value that isn't assigned to a variable.
Taking your code, this is approximately what the computer will try to do:
printf("%d",++(i+j));
// expanded step 1
printf("%d",++(0+1));
// expanded step 2
printf("%d",++(1));
As you can see in the last version, you are trying to call ++1, which is invalid.
In order to increment a value using ++, the operand must have integral, floating, or pointer type and must be a modifiable l-value expression (an expression without the const attribute):
int x = i+j;
printf("%d",++x);
Alternatively, you can use the addition operator:
printf("%d",i+j+1);
You cant increment the result.
Instead:
printf("%d",i + j + 1);
In C you cannot apply an unary operator too a number. When the value between the parentesis is evaluated first... before use the unary operation. assing the result to a variable.
#include<stdio.h>
int main(){
int i=0,j=1, other_value=0;
other_value = i+j;
printf("%d",++(other_value));
return 0;
}
Your previus code will work will get the error
error: lvalue required as increment operand
An "lvalue" is a value that can be the target of an assignment. The "l" stands for "left", as in the left hand side of the equals sign. An rvalue is the right hand value and produces a value, and cannot be assigned to directly. If you are getting "lvalue required" you have an expression that produces an rvalue when an lvalue is required.
in your case when compiler manipulate the expression then it will result to invalid expression which is ++1
try below:
printf("%d",i+j+1);
The expression ++(i+j) is not assignable. Here, the expression (i+j) will get evaluated first. For the ++ operator, the operand must have integral, floating, or pointer type and must be a modifiable l-value expression. Since the result from i+j is a constant value, so you get the error lvalue required as increment operand. You can choose to modify this expression as i+j+1.

GCC Error: lvalue required as left operand of assignment

I have a char* pointer and when I do (++pointer)= NULL, there's an error:
lvalue required as left operand of assignment
What does it mean and how do I fix it? The code is
void func(const char*p1, const char *p2){
char * pointer;
pointer=malloc(strlen(p1)+strlen(p2)+2;
(++pointer)=NULL;
}
The symbol pointer is an lvalue and can be used in an assignment on the left hand side. (++pointer), however, is not an lvalue and cannot be used in the same assignment.
If on an alien planet far far away it did compile, this code would increment pointer by one and then set it to zero (NULL) so to achieve the same effect, and be portable to earth compilers, use pointer = NULL;
The subexpression (++pointer) will evaluate to an rvalue and it can't be a left operand of assignment (=) operator.
Some expressions cannot be an assignable l-value.
In your code (++pointer) is an expression, which cannot be used as a left operand of the = sign.
Think of something like
(x + 2) = 5 // (x + 2) is an expression
Does it makes sense?

Address operator "&" with function returns

This:
bit_is_set(optos(),opt)
expanding macro bit_is_set:
((*(volatile uint8_t *)(((uint16_t) &(optos())))) & (1 << (opt)))
is not working, with error message: lvalue required as unary '&' operand.
But this:
uint8_t a=optos();
bit_is_set(a,opt)
works fine.
Why?
How do I use the address operator "&" with function returns?
For the same reason why this won't work:
uint8_t optos() {
return 4;
}
int main(void) {
uint8_t* addr = &optos();
return 0;
}
error: lvalue required as unary ‘&’ operand
The & operand requires an lvalue to return an address. A temporary rvalue (in your case the value returned from optos()) can't have its address taken and needs to be bound to a lvalue first.

Resources