【问题标题】:23warning: assignment makes pointer from integer without a cast [duplicate]23警告:赋值使指针从整数而不进行强制转换[重复]
【发布时间】:2010-04-25 17:42:00
【问题描述】:

我是使用数组和文件编程 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;
}

【问题讨论】:

  • 它是否表示行号?那条线上是什么?
  • 另外,在提交问题之前减少示例代码是有用的,通过删除无关的位;这可以很容易地简化为一个五行示例。
  • 与您的问题无关,但绝不,永远使用gets
  • jamesdlin 你能说得更具体点吗?为什么不?我只是一个初学者,不知道很多提示和技巧。
  • 您使用的是 IDE 还是程序员的编辑器?如果是这样,你能告诉我们哪一行有错误吗?

标签: c arrays file warnings


【解决方案1】:

这里缺少括号:

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 )

【讨论】:

  • (为了 OP 的利益)将赋值或其他“副作用”混合到这样的条件语句中通常被认为是不好的编程风格,这个问题是为什么它不好的一个典型例子。如果您将 fp=fopen() 移到上一行(在 if 语句之外),那么您“一次只做一件事”,对于任何级别的程序员来说,它都将更具可读性。
  • 是的,你是正确的。但它仍然有同样的警告!
  • 杰森不错的提示!到目前为止,我认为这种方式是“良好的编程风格”......谢谢提示。对于这个问题,即使进行了更改,我也会收到相同的警告
  • @fil:请更新您的新代码和新的错误消息。另外,程序的行为有什么变化吗?除了编译器警告之外,这些更改应该已修复它以使其正常工作。
  • 我得到了完全相同的警告,potatoswatter!程序的行为没有任何变化。
【解决方案2】:

首先,如果您尝试将文件精简为最小示例,那么调试此问题以及其他人帮助您会更容易。除此之外,如果这个

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

给你一个“赋值使指针从整数没有强制转换”警告,那么我怀疑你在范围内没有fopen 的声明。在第一次使用fopen 时,您是否有机会收到有关“隐含声明”的警告?如果是这样,那么您不知何故没有正确的fopen 声明可用,C 自动决定它必须返回一个整数。

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

#include<stdio.h>

真的在你的源代码中是怎样出现的?通常,我希望

#include <stdio.h>

我现在不记得 C 是否应该坚持在 #include&lt;stdio.h&gt; 之间留一个空格。

【讨论】:

  • 不,我没有收到“隐含声明”警告。这只是复制粘贴的错误。已编辑
  • include 语句都有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-22
相关资源
最近更新 更多