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

3

6

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?

Vicky answered 7/1, 2017 at 15:42 Comment(3)
You forgot to #include <string.h>Paraclete
Oh.. Thats it :+1Vicky
Take your compiler's warning serious.Anting
B
12

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

Bibliotheca answered 7/1, 2017 at 15:44 Comment(5)
Thanks @P.P. Solved.Vicky
Just trying to get some clarification, standard also explicitly mentions the words "implicit function declaration", isn't it?Weiman
But I didn't claim to quote the standard. The rule that involves both implicit function dec and implicit type for params -- both assumed to be int is commonly called as implicit int rule. Both of them have been referred so and removed in C99.Bibliotheca
OK sir, just to get my facts correct then, implicit int rule is not only for omitted type, it also includes the implicit function declaration, right?Weiman
Also, you mentioned I said "implicit int rule" (as used in the C standards) but I can't seem to find a usage, can you please refer to the usage, please? Thanks very much. :)Weiman
W
4

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
Weiman answered 7/1, 2017 at 15:45 Comment(0)
F
2

You need to include string.h

#include <string.h>
Fluency answered 7/1, 2017 at 15:46 Comment(1)
Yeah. Thanks I got itVicky

© 2022 - 2024 — McMap. All rights reserved.