c++ - 需要作为一元 ‘&’ 操作数的 c 左值中的打印错误

标签 c++ c

编译时出现代码错误

#include <stdio.h>      // standard input / output functions
#include <stdlib.h>
#include <string.h>     // string function definitions
#include <unistd.h>     // UNIX standard function definitions
#include <fcntl.h>      // File control definitions
#include <errno.h>      // Error number definitions
#include <termios.h>   

void main()
{
int USB = open( "/dev/ttyUSB0", O_RDWR| O_NOCTTY );         
struct termios tty;
struct termios tty_old;
memset (&tty, 0, sizeof tty);
/* Error Handling */
if ( tcgetattr ( USB, &tty ) != 0 ) 
{
printf("Error %d form tcgetattr : %d /n",&errno,strerror(errno));
}
}

错误是

USB2UART.c:18:49: error:lvalue required as unary ‘&’ operand
printf("Error %d form tcgetattr : %d /n",&errno,&strerror(errno));
                                         ^

我正在使用 USB 到 UART 的转换,并试图在其处理程序中出错。 希望有人能帮忙。谢谢:)

最佳答案

看起来您正在尝试将 & 应用于文字,而它应用于变量而不是文字。尝试删除 & 然后试一试。

C++ 标准,§5.3.1,3 说:

The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue [...]

关于c++ - 需要作为一元 ‘&’ 操作数的 c 左值中的打印错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33073727/

相关文章:

c++ - 字符串化任意数量的变量

c++ - 从模板函数调用的模板类的模板成员函数

c++ - 轻量级 http 服务器 C++

c - 将 C 代码转换为仅使用 goto

c - 程序收到信号SIGSEGV、段错误、链表程序

c++ - 层次结构中的成员函数指针

c++ - 将未知大小的二维数组传递给 C++ 中的函数

c - 如何运行另一个程序的多个副本 - C

c - Linux c fork - 进程不停止

c - 链表 : how to delete last node, 我们有指向单个链表中最后一个节点的指针