warning: assignment from incompatible pointer type

2

Where am I missing?

int (*funcion_c)(void*,void*);

int f_comparador(int* n1, int* n2){
     //Implementación...
}

funcion_c=f_comparador;  //WARNING
    
asked by G.G. 09.10.2017 в 22:43
source

1 answer

3

In one very simple thing: void * is not the same as int * .

While void * can be used, in the function declaration, to indicate any type of pointer , this does not happen like this in the pointers declaration to functions . In these cases, the match must be exact .

You have several solutions:

  • change the type of your variable pointer to function . It would be the most correct :

    int (*funcion_c)(int*,int*);
    
  • Change the type of your function ... and your body , of course:

    int f_comparador(void* n1, void* n2) { ...
    
  • There are more solutions. I only recommend them if you know very well what you are doing. All of them are based on the principle of aqui mando yo . When the compiler warns ...

  • Make a explicit cast:

    funcion_c = (int(*)(void*,void*))f_comparador;
    
  • Use something intermediate. The union are very useful for these things:

    union {
      int ( *fv )( void *, void * );
      int ( *fi )( int *, int * );
    } paf;
    
    paf.fi = f_comparador;
    
    paf.fv( &v1, &v2 );
    
  • Leave it as is. It's a warning , not an error . Depending on what you want to do, it will work ... or not; -)

  • I insist : the last 3 are for exceptional cases, and you have to know what you're doing. The functions work well with the correct types. Calling a function with an inappropriate argument to the bad ... usually produces curious results: -)

        
    answered by 10.10.2017 / 06:17
    source