一对指向采用不同类型参数的不同函数的指针可以兼容吗?

Can a pair of pointers to different functions taking different types of parameters be compatible?

我们能否将特定签名的函数地址放入定义为具有其他签名的函数指针中并无缝使用它?

例如下面的代码

#include <stdio.h>

void print_n(int *pn) {
    printf("%d\n", *pn);
}

void print_n_wrapper(void *p) {
    print_n(p);
}

int main(void) {
    int n = 123;
    void (*f)(void *) = print_n_wrapper;
    f(&n);
    f = print_n;
    f(&n);
    return 0;
}

在我的机器上编译并运行良好。我是否以某种方式调用了未定义的行为?

print_n_wrapper 中对 print_n(p) 的调用是 定义的,因为您所做的只是转换一个 void*,它最初是一个 int*int*

作业f = print_n;会给你带来麻烦。虽然赋值 定义的,但后续调用 f(&n) 的行为是 未定义。

是的,是undefined behaviour

引用 C11,章节 §6.3.2.3,指针,(强调我的

A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer. If a converted pointer is used to call a function whose type is not compatible with the referenced type, the behavior is undefined.

关于"function whose type is not compatible"部分,兼容性的定义是

For two function types to be compatible, both shall specify compatible return types.(146) Moreover, the parameter type lists, if both are present, shall agree in the number of parameters and in use of the ellipsis terminator; corresponding parameters shall have compatible types.

也就是说,void *int *应该是相同的类型,但不是 .所以,这些函数也不是兼容类型。