Search code examples
carraysmallocfunction-pointersrealloc

How do I realloc an array of function pointers?


Straight to the code:

#define PRO_SIGNAL( func, param ) (*func)(param)
void PRO_SIGNAL( paint[0], Pro_Window* );

signal->paint = realloc( signal->paint, sizeof( void (*)(Pro_Window*) ) * signal->paint_count );

The Error:

error: incompatible types when assigning to type 'void (*[])(struct Pro_Window *)' from type 'void *'|

Solution

  • It appears that you are assigning to an array not a pointer.

    From your output error message:

    'void (*[])(struct Pro_Window *)' from type 'void *'| 
    

    Note the [] in there (and it certainly isn't a lambda!) rather than a *

    If this is an "extendable" struct you need to realloc the entire struct not just the array member.

    By the way, a tip: if realloc fails it returns a NULL pointer and if you assign it to the variable that was being realloc'ed, the original memory it was pointing to will be lost forever. So always realloc into a temp first, check the value, and then assign back to the original pointer if it worked.