简体   繁体   中英

Warning: comparison between pointer and integer

I have a function like :

char * s;
{
    char * i = s, * t;

    for(t = s;*t != NULL;t++)
        if (*t == '/') i = t+1;

    return i;
}

The warning I get is comparison between pointer and integer. How can U resolve it?

Stop treating NULL as a character, it's supposed to be a pointer-type constant.

Your loop header should be:

for(t = s; *t != '\0'; ++t)

This is the "overly explicit" way of writing it, some C programmers aiming for maximum terseness will just say:

for(t = s; *t; ++t)

Since a comparison to '\\0' is the same as a comparison to 0, which is implicit in just evaluating the expression. However, I think the != '\\0' helps in reading since it makes it clear what's going on, we're looking for the string terminator '\\0' . The comparison should be trivially optimized out by any sane compiler.

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