23警告:赋值使得整数指针没有强制转换
我是编程c的新手,带有数组和文件。 我只是试图运行以下代码,但我得到这样的警告:
23 44警告:赋值使用整数而不使用强制转换
53错误:’char’之前的预期表达式
有帮助吗? 这可能很傻……但是我找不到什么是错的。
#include FILE *fp; FILE *cw; char filename_game[40],filename_words[40]; int main() { while(1) { /* Input filenames. */ printf("\n Enter the name of the file \n"); gets(filename_game); printf("\n Give the name of the file2 \n"); gets(filename_words); /* Try to open the file with the game */ fp=fopen(/* args omitted */); //line23** if (fp!= NULL) { printf("\n Successful opening %s \n",filename_game); fclose(fp); puts("\n Enter x to exit,any other to continue! \n "); if ( (getc(stdin))=='x') break; else continue; } else { fprintf(stderr,"ERROR!%s \n",filename_game); puts("\n Enter x to exit,any other to continue! \n"); if (getc(stdin)=='x') break; else continue; } /* Try to open the file with the names. */ //line 44** cw=fopen(/* args omitted */); if ( cw!=NULL ) { printf("\n Successful opening %s \n",filename_words); fclose(cw); puts("\n Enter x to exit,any other to continue \n "); if ( (getc(stdin))=='x') break; //line 53** else continue; } else { fprintf(stderr,"ERROR!%s \n",filename_words); puts("\n Enter x to exit,any other to continue! \n"); if (getc(stdin)=='x') break; else continue; } } return 0; }
你在这里缺少括号:
if (fp=fopen("crypt.txt","r")!=NULL)
!=
运算符的优先级高于=
因此编译器会看到如下表达式:
if ( fp = ( fopen("crypt.txt","r") != NULL ) )
fp
1或0取决于fopen
是否返回NULL
。 fp
是一个指针,0/1是一个整数,因此是警告。
你要
if ( ( fp=fopen("crypt.txt","r") ) != NULL )
首先,如果您尝试将文件剥离到最小示例,则可以更轻松地调试此问题,并让其他人帮助您。 除此之外,如果这样
fp = fopen( /* args omitted */ );
给你一个“赋值使得整数没有强制转换的指针”警告,然后我怀疑你没有在范围内声明fopen
。 你是否有机会在首次使用fopen
附近发出“隐含声明”的警告? 如果是这样,那么你以某种方式没有正确的fopen
可用声明,并且C自动决定它必须返回一个整数。
我不知道为什么会发生这种情况,除了你的示例代码的第一行看起来有点奇怪:
#include
这真的是它在源代码中的显示方式吗? 通常,我期待
#include
如果C应该坚持#include
和
之间的空格,我现在不记得了。