C:警告:赋值使得整数指针没有强制转换[默认启用]

时间:2017-01-07 15:42:51

标签: c strtok

这是我的代码

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

3 个答案:

答案 0 :(得分:10)

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

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

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

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

答案 1 :(得分:4)

strtok()<string.h>中的原型,您需要包含它。

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

FWIW,隐式函数声明现在根据最新标准无效。引用C11,Foreward,&#34;第二版中的主要更改包括:&#34;

  
      
  • 删除隐式函数声明
  •   

答案 2 :(得分:2)

您需要加入foreach ($system in $systemlist) { start-job -scriptblock{$system = Add-InformationToServerObj $system } }

string.h