简体   繁体   中英

C compiler: “Types 'char *' and 'int' are not assignment-compatible” - but there's no int?

I'm porting a FOSS package to HP-UX. I'm down to one compiler warning I just cannot figure out. I should note I'm using HP-UX's bundled free /usr/bin/cc, not the add-on purchased compiler, in order to make the package as widely usable as possible.

The compile (which is just cc with no flags) says:

Warning 942: "grep.c", line 288 # Types 'char *' and 'int' are not assignment-compatible.
        if ((fname = strsep(&lp, ":")) == NULL)
             ^^^^^^^^^^^^^^^^

fname in that function is declared:

    char    *fname, *line, *lp, *ln;

So fname is a char ptr. And that's what strsep() returns:

char *strsep(char **stringp, const char *delim) {

strsep is included from OpenBSD: http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/string/strsep.c?rev=1.6;content-type=text%2Fplain

So...strsep returns a char pointer, and that's what fname is. strsep can only return a character pointer or NULL. So where is the 'int' in all of this?

Did you #include a header file that properly defines strsep() ? If not, then the traditional thing to do is assume that the function returns int , which is what's probably causing your issue.

I'd guess that your headers aren't declaring strsep at all since it is non-standard, that would lead the compiler to assume that it returns int and there's your error. Try adding -D_BSD_SOURCE to your compiler options (or isolate that to only apply to string.h . If that doesn't work, grep through the system headers (or the man pages) to figure out if there is a prototype for strsep anywhere and what sort of macro dance you need to get at it. As a last resort (assuming you have it in your libc) you can prototype it yourself.

Also, I think you can replace strsep with the standard strtok with some modifications to the surrounding code. They serve the same purpose but do it with slightly different interfaces.

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