首页 > 解决方案 > 添加到“my_header.h”会导致错误消息

问题描述

我正在尝试从errno.hmy_func 中打印“错误号”。如果我<errno.h>直接包含在 my_func.c 中,一切正常。但是如果我包含<errno.h>"my_header.h"然后包含"my_header.h"在 my_func.c 编译器会吐出错误:

src/my_func.c: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'int (*(*)())' [-Wint-conversion] return (print_errno(errno));


/usr/include/sys/errno.h:81:15: note: expanded from macro 'errno' #define errno (*__error())

my_func.c:

#include "my_header.h"

int my_func(void)
{
  if (write(5, "Hello, world!", 13) == -1)
     return(print_errno(errno));
}

my_header.h:

#include <errno.h>

int print_errno(int errno);

print_errno.c:

#include "my_header.h"
#include <stdio.h>

int print_errno(int errno)
{
  printf("error number = %d", errno);
  return (-1);

}

为什么我有这个错误?

标签: errno

解决方案


这是因为您已经命名errno了由预处理器扩展的参数,因为这个

 #define errno (*__error())

(错误号.h)

所以这个原型

int print_errno(int errno);

扩展到

int print_errno(int (*__error()));

简短的修复,不要调用你的参数errno


推荐阅读