2017-01-07 70 views
2

这是我的代码C:警告:赋值时将整数指针没有施放[默认启用]

#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*。那么为什么会出现这种警告呢?

+2

你忘了' #include ' – EOF

+0

哦..就是这样:+1 – jophab

+0

拿你的编辑勒尔的警告很严重。 – alk

回答

6

包括#include <string.h>以获得strtok()的原型。

假设strtok()的编译器(就像在C99以前的C中一样)返回一个int。但不提供函数声明/原型在现代C中无效(自C99以来)。

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

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

+0

谢谢@ P.P。解决了。 – jophab

+0

只是试图得到一些澄清,标准也_explicitly_提到单词“隐函数声明”,不是吗? –

+0

但我没有声称引用标准。包含隐式函数dec和隐式params类型的规则 - 都假定为int,通常称为隐式int规则。他们两人都是在C99中被提及并删除的。 –

4

strtok()<string.h>原型,你需要把它列入。

否则,将缺乏前瞻性声明,你的编译器假定任何使用功能,其签名是不知道它,返回int并接受任意数量的参数。这调用implicit declarration

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

  • 删除隐函数声明
2

您需要包括string.h

#include <string.h> 
+0

是的。谢谢,我知道了 – jophab

相关问题