我一直在玩 C,不管怎样,我在想文件指针(指向结构类型)如何立即测试是否为 NULL:
FILE *cfPtr;
if ( ( cfPtr = fopen( "file.dat", "w" ) ) == NULL )
我自己尝试这样做,但出现错误。
struct foo{
int x;
};
struct foo bar = {0};
if (bar == NULL)
puts("Yay\n");
else
puts("Nay");
error C2088: '==' : illegal for struct
这是 stdio.h 文件中的 FILE 减速:
struct _iobuf {
char *_ptr;
int _cnt;
char *_base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char *_tmpfname;
};
typedef struct _iobuf FILE;
最佳答案
它应该是一个指向结构的指针:
struct foo* bar = ...
if (bar == NULL)
puts("Yay\n");
else
puts("Nay");
确实可以测试指向结构的指针是否为空,就像指向原始类型的指针一样。
关于c - 如何测试 FILE*(指向结构的指针)(如果 == NULL)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2957549/