user3810155
user3810155

Reputation:

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

Can we put a function address of a particular signature into a function pointer which is defined to have some other signature and use it seamlessly?

For example, the following code

#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;
}

gets compiled and runs fine in my machine. Am I invoking undefined behavior somehow?

Upvotes: 5

Views: 84

Answers (2)

Sourav Ghosh
Sourav Ghosh

Reputation: 134326

Yes, it is undefined behaviour.

Quoting C11, chapter §6.3.2.3, Pointers, (emphasis mine)

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.

and regarding the "function whose type is not compatible" part, the definition of compatibility goes like

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.

That means, void * and int * should have been the same types, but they are not. So, the functions are also not of compatible type.

Upvotes: 8

Bathsheba
Bathsheba

Reputation: 234705

The call to print_n(p) in print_n_wrapper is defined since all you're doing is converting a void* which was originally an int* to an int*

The assignment f = print_n; will give you trouble. Although the assignment is defined, the behaviour on the subsequent call f(&n) is undefined.

Upvotes: 6

Related Questions