为什么在我的程序中使用条件运算符给出警告“指针/整数类型不匹配”?

时间:2013-05-03 13:31:54

标签: c warnings

这是我的计划:

int main()
{
printf("%d : %s\n", errno, (errno==0)?"no error":strerror(errno));
}

它在编译时发出警告:

warning: pointer/integer type mismatch in conditional expression [enabled by default]

由于errno的类型为int且表达式为“无错误”且strerror()都返回指向字符串的指针,为什么会出现错误?

1 个答案:

答案 0 :(得分:2)

我怀疑#include <string.h>未包含在内,这意味着strerror()为其生成了一个隐式函数声明,返回int

此代码(http://ideone.com/6BckJx):

#include <stdio.h>
#include <errno.h>

int main()
{
    printf("%d : %s\n", errno, (errno==0)?"no error":strerror(errno));
    return 0;
}

产生

prog.c: In function ‘main’:
prog.c:6:5: error: implicit declaration of function ‘strerror’ 
    [-Werror=implicit-function-declaration]
prog.c:6:53: error: pointer/integer type mismatch in conditional expression
    [-Werror]
cc1: all warnings being treated as errors

添加#include <string.h>可以解决问题(http://ideone.com/Ihycd0)。