【发布时间】:2013-06-18 16:23:33
【问题描述】:
根据手册页strerror(errnum) 返回char *,但我收到以下警告:
gcc temp.c -o temp
temp.c: In function ‘mystrerror’:
temp.c:10:4: warning: return makes pointer from integer without a cast [enabled by default]
当我使用./temp 0 而不是./temp 256 运行它时,我遇到了段错误。
有人可以解释为什么会发生这种情况以及如何解决它(如果可能的话)?
温度.c
#include <stdio.h>
#include <errno.h>
char *mystrerror(int errnum)
{
switch (errnum) {
case 256:
return "My test";
default:
return strerror(errnum);
}
}
int main(int argc, char **argv)
{
int err;
if (argc > 1) {
err = atoi(argv[1]);
printf("test error (%d) %s\n", err, mystrerror(err));
}
return 0;
}
【问题讨论】:
-
您是否包含
#include <string.h>并尝试过? -
我不记得了; -Wall 会警告你在 C 中使用了未声明的函数吗?
-
还有
#include <stdlib.h>foratoi -
@Wug 确实如此。
-
-Wall 再次拯救了这一天。