如何在 C 中定义函数数组
我有一个结构体,其中包含这样的声明:
void (*functions[256])(void) //Array of 256 functions without arguments and return value
在另一个函数中我想定义它,但有 256 个函数! 我可以做这样的事情:
struct.functions[0] = function0;
struct.functions[1] = function1;
struct.functions[2] = function2;
等等,但这太累了,我的问题是有什么方法可以做这样的事情吗?
struct.functions = { function0, function1, function2, function3, ..., };
编辑:语法错误已纠正,如 Chris Lutz 所说。
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!
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,你没有。这是一个语法错误。您正在寻找:
这是一个函数指针数组。但请注意,
void func()
不是“不带参数且不返回任何内容的函数”。它是一个接受未指定数量或类型的参数并且不返回任何内容的函数。如果你想要“无参数”,你需要这样:在 C++ 中,
void func()
does 意味着“不带参数”,这会导致一些混乱(特别是因为 C 的功能指定void func()
的值是可疑的。)无论哪种方式,您都应该
typedef
您的函数指针。它将使代码变得非常容易理解,并且您只有一次机会(在typedef
处)出现语法错误:无论如何,您不能分配给数组,但您可以可以初始化一个数组并复制数据:
No you don't. That's a syntax error. You're looking for:
Which is an array of function pointers. Note, however, that
void func()
isn't a "function that takes no arguments and returns nothing." It is a function that takes unspecified numbers or types of arguments and returns nothing. If you want "no arguments" you need this:In C++,
void func()
does mean "takes no arguments," which causes some confusion (especially since the functionality C specifies forvoid func()
is of dubious value.)Either way, you should
typedef
your function pointer. It'll make the code infinitely easier to understand, and you'll only have one chance (at thetypedef
) to get the syntax wrong:Anyway, you can't assign to an array, but you can initialize an array and copy the data:
我遇到了同样的问题,这是我的小程序来测试解决方案。它看起来非常简单,所以我想我会分享给未来的访客。
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.
您可以动态地执行...这是一个使用 malloc 分配的动态函数数组的小示例...
You can do it dynamically... Here is a small example of a dynamic function array allocated with malloc...
来自我的头顶并且未经测试。
编辑:更正了 Chris Lutz 所说的语法错误。
From the top of my head and untested.
EDIT: Corrected syntax error as said by Chris Lutz.
您可以在声明结构实例时执行此操作:
在声明数组后,您不能使用此快捷方式来初始化数组:如果您需要这样做,则必须动态执行此操作(使用循环、memcpy 或其他)。
You could do that while declaring your struct instance:
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).如果您想使用类似
{func1, func2, ...}
的形式后初始化数组,可以通过以下方式完成(使用 GCC):< strong>UPD (感谢 Chris Lutz 的评论)
定义一个这样的宏:
并传递源代码使用 复合文字 向量,如下所示:
If you want to post-initialize an array using form like
{func1, func2, ...}
, this can be accomplished in the following way (using GCC):UPD (thanks to Chris Lutz for remarks)
Define a macro like this:
And pass source vector using Compound Literals, as follow: