• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

272
Views
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?

over 3 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 3 years ago · Santiago Trujillo Report

0

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.

over 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error