C:警告:赋值从整数生成指针而不进行强制转换[默认启用]

C : warning: assignment makes pointer from integer without a cast [enabled by default]

这是我的代码

#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 <string.h> 以获得 strtok() 的原型。

您的编译器(就像在 C99 之前的 C 中一样)因此假设 strtok() returns 和 int。但不提供函数 declaration/prototype 在现代 C 中无效(自 C99 起)。

C 中使用了一条旧规则,允许隐式函数声明。但是implicit int rule从C99开始就已经从C语言中移除了

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

strtok()原型在<string.h>,你需要包含它。

否则,由于缺少前向声明,您的编译器假定任何函数所使用的签名未知,returns int 并接受任意数量的参数。这个 称为 implicit declarration .

FWIW,根据最新标准,隐式函数声明现在无效。引用 C11,前言,"Major changes in the second edition included:"

  • remove implicit function declaration

您需要包括 string.h

#include <string.h>