简体   繁体   中英

if ( (my_fgets(line, max, fp)) == NULL) warning :comparison between pointer and integer [enabled by default]

this is my code:

my_fgets.c

#include<stdio.h>

char *my_fgets(char *s,int maxlen,FILE *fp)
{
    register int c;
    register char *cs;

    cs = s;
    while(--maxlen >0 && (c = getc(fp)) != EOF)
        if((*cs++ = c) == '\n')
            break;
    *cs = '\0';
    return (c == EOF && cs == s)? NULL: cs;
}

my_getline.c

#include<stdio.h>
#include<string.h>

/* getline : read a line, return lenght */ 
int my_getline(char *line, int max,FILE *fp)
{
    if ( (my_fgets(line, max, fp)) == NULL)
        return 0;
    else
        return strlen(line);
}

When I compiled the my_getline.c , I got a warning.

[root@server0 fgets]# gcc -c my_getline.c
my_getline.c: In function ‘my_getline’:
my_getline.c:7:36: warning: comparison between pointer and integer [enabled 
by default]
     if ( (my_fgets(line, max, fp)) == NULL)
                                    ^

I don't know where this program have errors. Could you tell me? Thank you!

You need to declare the function in either a heade file or inline in my_getline.c :

char *my_fgets(char *s,int maxlen,FILE *fp);

Is the declaration. Either put that before your my_getline function or in my_fgets.h (with include guards) and #include that header in both your .c files.

Without it the compiler assumes any function it sees without a declaration returns int

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM