Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C error: lvalue required as unary '&' operand

Tags:

c

lvalue

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!

like image 701
Shan Avatar asked Sep 10 '13 20:09

Shan


People also ask

What is the error lvalue required in C?

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()

What is the meaning of lvalue required?

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.


1 Answers

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.

like image 103
Carl Norum Avatar answered Sep 29 '22 23:09

Carl Norum