c - 我如何将函数指针传递给函数。

标签 c struct function-pointers

嘿,所以我想弄清楚如何将函数指针传递给存储在结构中的函数。以下是typedef

struct menu_item
{
    char name[ITEM_NAME_LEN+1];
    BOOLEAN (*func)(struct vm*);

};

我试图传递的函数具有以下原型(prototype)。

void print_list(struct vm_node  *root);

文件的定义是:

void print_list(struct vm_node  *root) {
    while (root) {
        printf("%s",root->data->id);
        root = root->next;
    }
    printf("\n");
}

最佳答案

struct menu_item item;
item.func = &print_list;

就这么简单。然而BOOLEAN (*func)(struct vm*);需要改为void (*func)(struct vm_node *);以匹配目标函数。

关于c - 我如何将函数指针传递给函数。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26228195/

相关文章:

c++ - "struct hack"可以这样实现吗?

你能在 C 中拥有带指定参数值的函数指针吗?

c - 为什么我在尝试传递数组值时收到 "incompatible pointer type"?

c - C 编程中 IF 语句的范围

c - 编写网络协议(protocol)

c 编程 : need fresh eyes to look at this [demo code ! = 作业]

c++ - 返回对结构的引用

c++ - 在 lambda 函数中捕获和传递参数之间的区别

c - 列出C程序中的多个接口(interface)

c - 将 '&&' 移动到开关中