【问题标题】:warning: comparison between pointer and integer in C警告:C 中指针和整数之间的比较
【发布时间】:2011-04-13 19:42:05
【问题描述】:

我收到警告

warning: comparison between pointer and integer

在下一段代码中包含if的行:

char cwd[1024];

if (getcwd(cwd, sizeof(cwd)) != (char*)NULL)
    printf("%s\n",cwd);
else
    error_print("error in pwd");

我需要如何解决以及需要解决什么问题?

【问题讨论】:

    标签: c pointers warnings getcwd


    【解决方案1】:

    您是否包含 unistd.h?如果不是,则会出现错误,因为您的 C 编译器假定 getcwd 返回 int。

    解决办法?包括 unistd.h

    【讨论】:

    • 我没有收到任何错误,只是警告,stdio.h 或 stdlib.h 中是否有任何原型定义 ------------------ ----------- int getcwd(char *buf, size_t size);
    【解决方案2】:

    getcwd的原型是

    char *getcwd(char *buf, size_t size);
    

    确保包含<unistd.h>,否则返回类型将默认为int

    在这里,即使是 Ideone 也给出了它的Current Working Directory

    【讨论】:

      【解决方案3】:

      您是否包含了必要的 .h,以便编译器了解 getcwd 返回的内容?

      你的 c 编译器的行为可能是从每个未定义的函数中假设一个 int 返回值。

      【讨论】:

        【解决方案4】:

        在以下链接的返回类型部分,getcwd 在失败时返回 null。因此,无需检查!= (char *)NULL,只需检查!= NULL

        http://linux.die.net/man/3/getcwd

        【讨论】:

        • 很好的做法,但不能解决问题。
        【解决方案5】:

        用这个if (getcwd(cwd, sizeof(cwd)) != NULL)修改行

        【讨论】:

        • 很好的做法,但不能解决问题。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-25
        • 2012-10-14
        • 1970-01-01
        • 2021-12-18
        • 1970-01-01
        相关资源
        最近更新 更多