如何重新分配函数指针数组?

时间:2021-02-04 19:57:43

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

3 个解决方案

#1


3  

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 *

注意那里的[](它肯定不是lambda!)而不是*

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.

顺便说一句,提示:如果realloc失败,它返回一个NULL指针,如果你将它分配给正在重新分配的变量,它指向的原始内存将永远丢失。所以总是首先重新分配到temp,检查值,然后分配回原始指针,如果它工作。

#2


1  

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.

你没有告诉我们singal-> paint的定义,但我从错误信息中推断出它被声明为一个函数指针数组,这意味着signal是一个带有flex数组的结构(paint [])。您无法分配给数组,您需要重新分配整个结构。

#3


0  

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.

所以,你要么试图像凯文所说的那样重新分配一个数组,要么就是你在C ++模式下进行编译,我相信这个模型不是隐含的。

Edit: I've added some error handling.

编辑:我添加了一些错误处理。

#1


3  

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 *

注意那里的[](它肯定不是lambda!)而不是*

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.

顺便说一句,提示:如果realloc失败,它返回一个NULL指针,如果你将它分配给正在重新分配的变量,它指向的原始内存将永远丢失。所以总是首先重新分配到temp,检查值,然后分配回原始指针,如果它工作。

#2


1  

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.

你没有告诉我们singal-> paint的定义,但我从错误信息中推断出它被声明为一个函数指针数组,这意味着signal是一个带有flex数组的结构(paint [])。您无法分配给数组,您需要重新分配整个结构。

#3


0  

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.

所以,你要么试图像凯文所说的那样重新分配一个数组,要么就是你在C ++模式下进行编译,我相信这个模型不是隐含的。

Edit: I've added some error handling.

编辑:我添加了一些错误处理。