警告:指针和整型之间的比较,传递' read '的参数1使指针的整型没有强制转换

warnings: comparison between pointer and integer, passing argument 1 of ‘read’ makes integer from pointer without a cast

本文关键字:整型 指针 转换 参数 read 比较 传递 警告 之间      更新时间:2023-10-16

在编译名为Online_ana.c的程序时(通过运行makefile)使用以下行:

******#define FIFO1 (getenv("fifo1"))******
FILE *fifoFile;
54 : if ((fifoFile = fopen((FIFO1!=NULL) ? FIFO1 : "fifo1", O_RDONLY)) == -1)
153 : if ((read(fifoFile, &BufferHeader, sizeof(struct event_header))) != sizeof(struct event_header))
168 : if ((read(fifoFile, &MyBuffer[cnt], 4*BufferHeader.n_sca)) != 4*BufferHeader.n_sca)
189 : if ((read(fifoFile, &MyBuffer[cnt], 2*(BufferHeader.n_adc+BufferHeader.n_tdc+BufferHeader.n_pcos))) != 2*(BufferHeader.n_adc+BufferHeader.n_tdc+BufferHeader.n_pcos))

I encountered such warnings:
Online_ana.c:**54**:69: warning: comparison between pointer and integer [enabled by default]
Online_ana.c: In function ‘GetBufferFromFifo’:
Online_ana.c:**153**:52: warning: passing argument 1 of ‘read’ makes integer from pointer without a cast [enabled by default]
/usr/include/unistd.h:361:16: note: expected ‘int’ but argument is of type ‘struct FILE *’
Online_ana.c:**168**:7: warning: passing argument 1 of ‘read’ makes integer from pointer without a cast [enabled by default]
/usr/include/unistd.h:361:16: note: expected ‘int’ but argument is of type ‘struct FILE *’
Online_ana.c:**189**:7: warning: passing argument 1 of ‘read’ makes integer from pointer without a cast [enabled by default]
/usr/include/unistd.h:361:16: note: expected ‘int’ but argument is of type ‘struct FILE *’

怎么了?希望有人能帮助我。提前感谢!!

大多数警告是因为您将FILE*(指针)传递给需要整数参数的函数。至于fopen的警告,这是因为将指针(FILE*)与整数(-1)进行比较。

换句话说,您正在混合两个不同的I/O系统。

要么使用POSIX函数,如openread,要么使用C函数fopenfread。不要混合。

您编写的代码就好像您正在使用UNIX文件描述符,而不是标准的I/O文件句柄。如果你想这样做,把fopen()改为open(),把fifoFile的声明从FILE *改为int

你正在使用fopen()来打开一个文件,这是一个标准库函数,它将给你一个与流相关的文件指针。另一方面,您正在使用read(),这是一个系统调用,将文件描述符作为其第一个输入,这是一个整数。

重要的是-流的模式必须与文件描述符的模式兼容。

虽然,也可以使用stdio.h

中的int fileno(FILE *stream)将FILE*转换为文件描述符(fd)。