如何在 C 中定义函数数组

发布于 2024-10-22 07:31:16 字数 500 浏览 2 评论 0原文

我有一个结构体,其中包含这样的声明:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

七堇年 2024-10-29 07:31:16

我有一个包含这样的声明的结构:

不,你没有。这是一个语法错误。您正在寻找:

void (*functions[256])();

这是一个函数指针数组。但请注意,void func() 不是“不带参数且不返回任何内容的函数”。它是一个接受未指定数量或类型的参数并且不返回任何内容的函数。如果你想要“无参数”,你需要这样:

void (*functions[256])(void);

在 C++ 中,void func() does 意味着“不带参数”,这会导致一些混乱(特别是因为 C 的功能指定 void func() 的值是可疑的。)

无论哪种方式,您都应该 typedef 您的函数指针。它将使代码变得非常容易理解,并且您只有一次机会(在 typedef 处)出现语法错误:

typedef void (*func_type)(void);
// ...
func_type functions[256];

无论如何,您不能分配给数组,但您可以可以初始化一个数组并复制数据:

static func_type functions[256] = { /* initializer */ };
memcpy(mystruct.functions, functions, sizeof(functions));

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." 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.)

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 the typedef) to get the syntax wrong:

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));
感情旳空白 2024-10-29 07:31:16

我遇到了同样的问题,这是我的小程序来测试解决方案。它看起来非常简单,所以我想我会分享给未来的访客。

#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;
}

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;
}
永不分离 2024-10-29 07:31:16

您可以动态地执行...这是一个使用 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;
}

You can do it dynamically... Here is a small example of a dynamic function array allocated with 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;
}
裸钻 2024-10-29 07:31:16

来自我的头顶并且未经测试。

// 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];

编辑:更正了 Chris Lutz 所说的语法错误。

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.

指尖上得阳光 2024-10-29 07:31:16

您可以在声明结构实例时执行此操作:

function_structur fs = { struct_field1,
                         struct_field2,
                         {function0, function1, ..., function255},
                         struct_field3,
                         ... };

在声明数组后,您不能使用此快捷方式来初始化数组:如果您需要这样做,则必须动态执行此操作(使用循环、memcpy 或其他)。

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).

花期渐远 2024-10-29 07:31:16

如果您想使用类似 {func1, func2, ...} 的形式初始化数组,可以通过以下方式完成(使用 GCC):

< strong>UPD (感谢 Chris Lutz 的评论)

定义一个这样的宏:

#define FUNCTION_VECTOR_COPY(destVec, sourceVec) memcpy(destVec, sourceVec, sizeof(sourceVec))

并传递源代码使用 复合文字 向量,如下所示:

#include <string.h>
...
void (*functions[256])();
...
FUNCTION_VECTOR_COPY (functions, ((void(*[])()) {func1, func2, func3}));

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:

#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}));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文