Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 使用C中存储在结构中的函数数组中的函数_Arrays_C_Structure_Function Pointers - Fatal编程技术网

Arrays 使用C中存储在结构中的函数数组中的函数

Arrays 使用C中存储在结构中的函数数组中的函数,arrays,c,structure,function-pointers,Arrays,C,Structure,Function Pointers,我声明了这样一个结构: typedef struct s_data { char buff[2048]; int len; void *func[10]; struct data *next; } t_data; 在我的代码中,当传递*数据时,我分配了一些函数(只给出一个,这样更容易理解) 我的第一个函数是取参数*数据和一个int。 然后,我尝试在另一个函数中使用这个函数,做 data->func[0](data, var

我声明了这样一个结构:

typedef struct s_data {

    char buff[2048];
    int len; 
    void *func[10];
    struct data *next;

}               t_data;
在我的代码中,当传递*数据时,我分配了一些函数(只给出一个,这样更容易理解)

我的第一个函数是取参数*数据和一个int。 然后,我尝试在另一个函数中使用这个函数,做

data->func[0](data, var);

我尝试了这个和其他几个涉及地址(*func[0])的语法,但都不起作用。我从那边其他更复杂的问题中了解到,我不应该像这样存储我的函数,或者应该将它转换为另一个typedef,但我并没有真正理解所有内容,因为我是编程新手。

如果您的所有函数都具有相同的签名,您可以这样做:

#include <stdio.h>

typedef void (*func)(void *, int);

struct s_data {
        char buff[2048];
        int len;
        func f[10];
        struct s_data *next;
};

static void
my_first_function(void *d, int x)
{
        (void)d;
        printf("%d\n", x + 2);
}

static void
init_data(struct s_data *data)
{
        data->len = 1;
        data->f[0] = my_first_function;
}

int
main(void)
{
        struct s_data d;
        init_data(&d);
        d.f[0](NULL, 5);
        return 0;
}
#包括
typedef void(*func)(void*,int);
结构s_数据{
charbuff[2048];
内伦;
func f[10];
结构s_数据*下一步;
};
静态空隙
我的第一个函数(void*d,int x)
{
(d)无效;
printf(“%d\n”,x+2);
}
静态空隙
初始化数据(结构s_数据*数据)
{
数据->len=1;
data->f[0]=我的第一个函数;
}
int
主(空)
{
结构s_数据d;
初始数据(&d);
d、 f[0](空,5);
返回0;
}

如果您的函数具有不同的签名,您可能希望使用联合,或者可能需要结构的多个不同成员来存储函数指针。

void*
只能可靠地用作通用对象指针(“指向变量的指针”)。不是作为泛型函数指针

但是,您可以安全地在不同的函数指针类型之间进行转换,只要您只调用具有正确类型的实际函数。因此,可以使用任何函数指针类型作为通用类型,如下所示:

void (*func[10])(void);
...
func[0] =  ((void)(*)(void))&myfirstfunction;
...
((whatever)func[0]) (arguments); // call the function
typedef void (*pointer_to_function)(void *, int);

struct s_data {
        char buff[2048];
        int len;
        pointer_to_function array_of_pointeters[10];
        struct s_data *next;
};

正如您可能注意到的,C中的函数指针语法非常糟糕。所以我建议使用typedefs:

typedef void genfunc_t (void);
typedef int  somefunc_t (whatever*); // assuming this is the type of myfirstfunction
这样代码就更容易读写了:

genfunc_t* func [10];
...
func[0] = (genfunc_t*)&myfirstfunction;
...
((somefunc_t*)func[0]) (arguments);

问题是您实际上没有声明函数指针数组。实际上,您所做的是一个指向void的指针数组

声明指向函数的指针的语法如下所示:

函数返回类型(*指针名称)(arg1类型,arg2类型,…)

然后可以创建指向函数的指针数组:

函数返回类型(*arr\u name[])(arg1\u类型,arg2\u类型,…)

因此,您的结构声明应如下所示:

void (*func[10])(void);
...
func[0] =  ((void)(*)(void))&myfirstfunction;
...
((whatever)func[0]) (arguments); // call the function
typedef void (*pointer_to_function)(void *, int);

struct s_data {
        char buff[2048];
        int len;
        pointer_to_function array_of_pointeters[10];
        struct s_data *next;
};


祝你好运:)

void*
是一个非类型指针。编译器不知道指针真正指向的是什么,这意味着您不能在没有转换到正确类型的情况下取消引用它。更好的解决方案是使
func
成为正确类型的数组。函数类型的类型别名(使用
typedef
创建)非常有用。谢谢@Someprogrammerdude!根据您的回答,我刚刚尝试将func数组更改为int数组,因为我的所有函数都将返回int。但当我尝试分配它时:data->func[0]=&myfirstfunction;,我收到了错误赋值的警告('int'来自'int()(t_data*,int)')。我想我从警告中了解到问题来自我的论点,但我遗漏了什么?谢谢你的时间,谢谢你,成功了!对于那些感兴趣的人,比如上面,我声明typedef void(*func)(void*,int);然后,func f[10];在我的结构中。在赋值过程中,我只需将我的函数转换成一个(func),以便data->f[0]=(func)&myfirstfunction;它成功了。再次感谢!我们刚刚在课堂上通过了这个主题,因此您的示例对我们很有帮助,您为什么需要执行(void)d,先生?@snr
(void)
强制转换是为了抑制编译器对未使用参数的警告。