如何将函数指针传递给函数。

时间:2014-10-07 03:39:40

标签: c struct function-pointers

嘿所以我试图找出如何将函数指针传递给存储在struct中的函数。以下是typedef

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

};

我想传递的功能有以下原型。

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");
}

2 个答案:

答案 0 :(得分:1)

struct menu_item item;
item.func = &print_list;

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

答案 1 :(得分:0)

函数有点像数组,所以你可以做到

menu_item.func = print_list;

就像你使用数组一样

int arr[20];
int *ptr;
ptr = array /*