【问题标题】:C : warning: assignment makes pointer from integer without a cast [enabled by default]C:警告:赋值使指针从整数而不进行强制转换[默认启用]
【发布时间】:2017-05-22 05:57:06
【问题描述】:

这是我的代码

#include<stdio.h>
#include<stdlib.h>

void main() {
    FILE *fp;
    char * word;
    char line[255];
    fp=fopen("input.txt","r");
    while(fgets(line,255,fp)){
        word=strtok(line," ");
        while(word){
            printf("%s",word);
            word=strtok(NULL," ");
        }
    }
}

这是我收到的警告。

token.c:10:7: warning: assignment makes pointer from integer without a cast [enabled by default]
   word=strtok(line," ");
       ^

token.c:13:8: warning: assignment makes pointer from integer without a cast [enabled by default]
    word=strtok(NULL," ");
        ^

word 被声明为char*。那么为什么会出现这个警告呢?

【问题讨论】:

  • 你忘了#include &lt;string.h&gt;
  • 哦.. 就是这样:+1
  • 认真对待编译器的警告。

标签: c strtok


【解决方案1】:

包含#include &lt;string.h&gt; 以获取strtok() 的原型。

因此,您的编译器(如 C99 之前的 C)假定 strtok() 返回 int。但不提供函数声明/原型在现代 C 中无效(自 C99 起)。

在 C 中使用了允许隐式函数声明的旧规则​​。但隐式 int 规则自 C99 以来已从 C 语言中移除。

见:C function calls: Understanding the "implicit int" rule

【讨论】:

  • 谢谢@P.P.解决了。​​
  • 只是想澄清一下,标准也明确地提到了“隐式函数声明”,不是吗?
  • 但我并没有声称引用该标准。涉及隐式函数 dec 和 params 的隐式类型的规则 - 都假定为 int 通常称为隐式 int 规则。它们都在 C99 中被提及并删除。
  • 好的,先生,为了让我的事实正确,隐式int规则不仅适用于省略类型,它还包括隐式函数声明,对吧?
  • 另外,你提到了我说的是“隐式int规则”(在C标准中使用)但我似乎找不到用法,你能参考一下吗请问用法?非常感谢。 :)
【解决方案2】:

strtok()&lt;string.h&gt; 的原型,你需要包含它。

否则,由于缺少前向声明,您的编译器假定任何用于其签名的函数都返回int 并接受任意数量的参数。这个称为implicit declarration

FWIW,根据最新标准,隐式函数声明现在无效。引用C11,Foreward,“第二版的主要变化包括:”

  • 移除隐式函数声明

【讨论】:

    【解决方案3】:

    您需要包含string.h

    #include <string.h>
    

    【讨论】:

    • 是的。谢谢我明白了
    猜你喜欢
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多