警告:指针和整型之间的比较,传递' read '的参数1使指针的整型没有强制转换
warnings: comparison between pointer and integer, passing argument 1 of ‘read’ makes integer from pointer without a cast
在编译名为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函数,如open
和read
,要么使用C函数fopen
和fread
。不要混合。
您编写的代码就好像您正在使用UNIX文件描述符,而不是标准的I/O文件句柄。如果你想这样做,把fopen()
改为open()
,把fifoFile
的声明从FILE *
改为int
。
你正在使用fopen()来打开一个文件,这是一个标准库函数,它将给你一个与流相关的文件指针。另一方面,您正在使用read(),这是一个系统调用,将文件描述符作为其第一个输入,这是一个整数。
重要的是-流的模式必须与文件描述符的模式兼容。
虽然,也可以使用stdio.h
相关文章:
- 为什么允许将整型、枚举和指向成员的指针类型reinterpret_cast到自身?
- 是否存在将长整型转换为指针有效的情况
- 类型转换无效指针指向无符号短整型
- 复制存储在空指针的整型数组到整型数组
- 警告:指针和整型之间的比较,传递' read '的参数1使指针的整型没有强制转换
- 将长整型转换为结构指针
- 使用整型指针作为唯一id有多糟糕?c++ 11
- 添加2个整型指针数组时产生垃圾整型
- c++中函数指针强制转换为整型
- 表达式在指针上必须具有整型或非整型枚举类型
- 指向char数组的整型指针
- 我可以通过类型特征确定指针是否为整型
- 将指向整型对象的指针数组的元素相加
- 引用整型数组的指针
- 为什么不允许使用指针+指针,而允许使用指针+整型
- 针对Intel x64的GCC中指向整型的指针的对齐
- 通过引用传递指向长整型指针
- 将双精度指针转换为整型指针
- 初始化指向整型数组的指针
- 整型数组指针在c++中的实现