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

Deadly 提交于 2019-12-23 15:43:38

问题


This is my code

#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," ");
        }
    }
}

This the warning I get.

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," ");
        ^

The word is declared as char*. Then why this warning arises?


回答1:


Include #include <string.h> to get the prototype for strtok().

Your compiler (like in pre-C99 C) assumed strtok() returns an int because of that. But not providing function declaration/prototype is not valid in modern C (since C99).

There used an old rule in C which allowed implicit function declarations. But implicit int rule has been removed from C language since C99.

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




回答2:


strtok() is prototyped in <string.h>, you need to include it.

Otherwise, with the lack of forward declaration, your compiler assumes that any function used for which the signature is not known to it, returns an int and accepts any number of parameters. This was called implicit declarration .

FWIW, implicit function declaration is now invalid as per the latest standard. Quoting C11, Foreward, "Major changes in the second edition included:"

  • remove implicit function declaration



回答3:


You need to include string.h

#include <string.h>


来源:https://stackoverflow.com/questions/41523180/c-warning-assignment-makes-pointer-from-integer-without-a-cast-enabled-by-de

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!