23warning:赋值使得整数指针没有强制转换

时间:2010-04-25 17:42:00

标签: c arrays file warnings

我是使用数组和文件编程c的新手。  我只是试图运行以下代码,但我得到这样的警告:

  

23 44 警告:赋值来自指针         没有强制转换的整数

     

53 错误:之前的预期表达式   “字符”

有任何帮助吗?这可能很愚蠢......但是我找不到什么是错的。

#include <stdio.h>

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;
}

2 个答案:

答案 0 :(得分:8)

您在这里缺少括号:

if (fp=fopen("crypt.txt","r")!=NULL)

!=运算符的优先级高于=,因此编译器会看到如下表达式:

if ( fp = ( fopen("crypt.txt","r") != NULL ) )

fp获得1或0,具体取决于fopen是否返回NULLfp是一个指针,0/1是一个整数,因此是警告。

你想要

if ( ( fp=fopen("crypt.txt","r") ) != NULL )

答案 1 :(得分:0)

首先,如果您尝试将文件剥离到最小示例,则可以更轻松地调试此问题,并让其他人帮助您。除此之外,如果这个

fp = fopen( /* args omitted */ );

给你一个“赋值使得整数没有强制转换的指针”警告,然后我怀疑你在范围内没有fopen的声明。你是否有机会在首次使用fopen附近时收到关于“隐含声明”的警告?如果是这样,那么你以某种方式没有正确的fopen声明,并且C自动决定它必须返回一个整数。

我不知道为什么会发生这种情况,除了示例代码的第一行看起来有些奇怪:

#include<stdio.h>

真的是它在源代码中的显示方式吗?通常情况下,我希望

#include <stdio.h>

我现在不记得,如果C应该坚持#include<stdio.h>之间的空格。