简体   繁体   中英

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 *'|

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.

You don't show us the definition of singal->paint , but I infer from the error message that it's declared as an array of function pointers, meaning signal is a struct with a flex array ( paint[] ). You can't assign to an array, you need to realloc the whole struct .

Not sure what you're trying to do, but this works perfectly here:

#include <stdlib.h>

int main(int argc, char ** argv)
{
        void (**foobar) (int a, int b);
        void (**tmp) (int a, int b);
        foobar = NULL;
        if (!(foobar = malloc(sizeof(*foobar)*4))
            return 1;
        if (!(tmp = realloc(foobar, sizeof(*foobar)*5)) {
            free(foobar);
            return 1;
        } else {
            foobar = tmp;
        }
        free(foobar);
        return 0;
}

So, either you're trying to realloc an array like Kevin says, or perhaps you're compiling in C++ mode, where I believe that the cast is not implicit.

Edit : I've added some error handling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM