Search code examples
ccastingtypedefgcc-warning

Conversion between pointer to function pointer error


So I am trying to address this warning: nonstandard conversion between pointer to function and pointer to data I haven't been able to figure out a good way to do this. This is all done in c and not c++.

Currently I have a header file with:

typdef struct myConnection_s
{
    ...
    void*   Callback
} myConnection_t, *Connection


typdef HRESULT (*HttpHook)(Connection, char*);

In other files foo.c I have:

....
Connection myConnection;
...

HttpHook myHook = (HttpHook) myConnection->Callback;
...
return myHook(.....);

Is there a good way to fix this warning without having to change too much? If not what would be the best way to rewrite this?

Thanks!


Solution

  • typedef struct myConnection_s
    {
        /* ... */
        HttpHook Callback;
    } myConnection_t, *Connection;
    

    You can also then drop the explicit cast later on:

    HttpHook myHook = myConnection->Callback;
    
    if (myHook)
        myHook(/* ... */);
    

    Edit: Looks like you have an ordering problem... try this:

    struct myConnection_s;
    
    typedef HRESULT (*HttpHook)(struct myConnection_s *, char*);
    
    typedef struct myConnection_s
    {
        /* ... */
        HttpHook Callback;
    } myConnection_t, *Connection;