简体   繁体   English

C编译器:“类型'char *'和'int'不是赋值兼容的” - 但是没有int?

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

I'm porting a FOSS package to HP-UX. 我正在将一个FOSS包移植到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. 我应该注意到我正在使用HP-UX捆绑的免费/ usr / bin / cc,而不是附加购买的编译器,以使包尽可能广泛使用。

The compile (which is just cc with no flags) says: 编译(只是没有标志的cc)说:

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: 声明该函数中的fname:

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

So fname is a char ptr. 所以fname是一个char ptr。 And that's what strsep() returns: 这就是strsep()返回的内容:

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 strsep包含在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返回一个char指针,这就是fname。 strsep can only return a character pointer or NULL. strsep只能返回一个字符指针或NULL。 So where is the 'int' in all of this? 那么所有这些中的'int'在哪里?

Did you #include a header file that properly defines strsep() ? 你是否#include一个正确定义strsep()的头文件? If not, then the traditional thing to do is assume that the function returns int , which is what's probably causing your issue. 如果没有,那么传统的做法就是假设函数返回int ,这可能会导致你的问题。

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. 我猜你的标题根本没有声明strsep因为它是非标准的,这将导致编译器假设它返回int并且你的错误。 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. 尝试将-D_BSD_SOURCE添加到您的编译器选项中(或者将其隔离到仅适用于string.h 。如果这不起作用,请通过系统头文件(或手册页)grep来确定是否存在strsep的原型,并且什么样的宏观舞蹈你需要得到它。作为最后的手段(假设你在你的libc中有它)你可以自己原型。

Also, I think you can replace strsep with the standard strtok with some modifications to the surrounding code. 另外,我认为您可以使用标准的strtok替换strsep ,并对周围的代码进行一些修改。 They serve the same purpose but do it with slightly different interfaces. 它们服务于相同的目的,但使用略有不同的接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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