[英]How to define an array of functions in C
I have a struct that contains a declaration like this one:我有一个包含这样一个声明的结构:
void (*functions[256])(void) //Array of 256 functions without arguments and return value
And in another function I want to define it, but there are 256 functions!在另一个函数中我想定义它,但是有 256 个函数! I could do something like this:
我可以做这样的事情:
struct.functions[0] = function0;
struct.functions[1] = function1;
struct.functions[2] = function2;
And so on, but this is too tiring, my question is there some way to do something like this?等等,但是这太累了,我的问题是有什么方法可以做这样的事情吗?
struct.functions = { function0, function1, function2, function3, ..., };
EDIT : Syntax error corrected as said by Chris Lutz.编辑:如 Chris Lutz 所说,语法错误已更正。
I have a struct that contains a declaration like this one:
我有一个包含这样一个声明的结构:
No you don't.不,你没有。 That's a syntax error.
那是语法错误。 You're looking for:
您正在寻找:
void (*functions[256])();
Which is an array of function pointers.这是一个函数指针数组。 Note, however, that
void func()
isn't a "function that takes no arguments and returns nothing."但是请注意,
void func()
不是“不带参数且不返回任何内容的函数”。 It is a function that takes unspecified numbers or types of arguments and returns nothing.它是一个函数,它接受未指定的数字或类型的参数并且不返回任何内容。 If you want "no arguments" you need this:
如果你想要“没有参数”,你需要这个:
void (*functions[256])(void);
In C++, void func()
does mean "takes no arguments," which causes some confusion (especially since the functionality C specifies for void func()
is of dubious value.)在 C++ 中,
void func()
确实意味着“不带参数”,这会引起一些混淆(尤其是因为 C 为void func()
指定的void func()
具有可疑的价值。)
Either way, you should typedef
your function pointer.无论哪种方式,您都应该
typedef
函数指针。 It'll make the code infinitely easier to understand, and you'll only have one chance (at the typedef
) to get the syntax wrong:这将使代码更容易理解,并且您只有一次机会(在
typedef
)弄错语法:
typedef void (*func_type)(void);
// ...
func_type functions[256];
Anyway, you can't assign to an array, but you can initialize an array and copy the data:无论如何,你不能分配给一个数组,但你可以初始化一个数组并复制数据:
static func_type functions[256] = { /* initializer */ };
memcpy(mystruct.functions, functions, sizeof(functions));
I had the same problem, this is my small program to test the solution.我有同样的问题,这是我测试解决方案的小程序。 It looks pretty straightforward so I thought I'd share it for future visitors.
它看起来很简单,所以我想我会为未来的访客分享它。
#include <stdio.h>
int add(int a, int b) {
return a+b;
}
int minus(int a, int b) {
return a-b;
}
int multiply(int a, int b) {
return a*b;
}
typedef int (*f)(int, int); //declare typdef
f func[3] = {&add, &minus, &multiply}; //make array func of type f,
//the pointer to a function
int main() {
int i;
for (i = 0; i < 3; ++i) printf("%d\n", func[i](5, 4));
return 0;
}
You can do it dynamically... Here is a small example of a dynamic function array allocated with malloc...你可以动态地做...这是一个用malloc分配的动态函数数组的小例子...
#include <stdio.h>
#include <stdlib.h>
typedef void (*FOO_FUNC)(int x);
void a(int x)
{
printf("Function a: %d\n", x);
}
void b(int x)
{
printf("Function b: %d\n", x);
}
int main(int argc, char **argv)
{
FOO_FUNC *pFoo = (FOO_FUNC *)malloc(sizeof(FOO_FUNC) * 2);
pFoo[0] = &a;
pFoo[1] = &b;
pFoo[0](10);
pFoo[1](20);
return 0;
}
From the top of my head and untested.从我的头顶开始,未经测试。
// create array of pointers to functions
void (*functions[256])(void) = {&function0, &function1, &function2, ..., };
// copy pointers to struct
int i;
for (i = 0; i < 256; i++) struct.functions[i] = functions[i];
EDIT : Corrected syntax error as said by Chris Lutz.编辑:更正了 Chris Lutz 所说的语法错误。
You could do that while declaring your struct instance:你可以在声明你的结构实例时这样做:
function_structur fs = { struct_field1,
struct_field2,
{function0, function1, ..., function255},
struct_field3,
... };
You cannot use this shortcut for initialize arrays after the array has been declared: if you need to do that, you'll have to do it dynamically (using a loop, a memcpy
or something else).在声明数组后,您不能使用此快捷方式来初始化数组:如果需要这样做,则必须动态执行(使用循环、
memcpy
或其他方法)。
If you want to post -initialize an array using form like {func1, func2, ...}
, this can be accomplished in the following way (using GCC):如果您想使用
{func1, func2, ...}
类的形式对数组进行后初始化,可以通过以下方式完成(使用 GCC):
UPD (thanks to Chris Lutz for remarks) UPD (感谢Chris Lutz的评论)
Define a macro like this:像这样定义一个宏:
#define FUNCTION_VECTOR_COPY(destVec, sourceVec) memcpy(destVec, sourceVec, sizeof(sourceVec))
And pass source vector using Compound Literals , as follow:并使用复合文字传递源向量,如下所示:
#include <string.h>
...
void (*functions[256])();
...
FUNCTION_VECTOR_COPY (functions, ((void(*[])()) {func1, func2, func3}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.