Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

397
Vistas
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 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

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 4 years ago · Santiago Trujillo Denunciar

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 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda