C语言 C 错误:左值需要作为一元“&”操作数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18727887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C error: lvalue required as unary '&' operand
提问by Shan
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:
CDC_Send_DATA 定义如下:
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!
希望有人能帮忙。谢谢!
回答by Carl Norum
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.
检查返回值也可能是一个好主意。

