C error: lvalue required as unary '&' operand

会有一股神秘感。 提交于 2020-01-12 23:15:27

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/18727887/c-error-lvalue-required-as-unary-operand

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!