简体   繁体   中英

How do i pass a function pointer to a function.

Hey so i am trying to figure out how i pass a function pointer to a function stored within a struct. The following is the typedef

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

};

The function i am trying to pass has the following prototype.

void print_list(struct vm_node  *root);

with the defintion of the file being:

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;

As simple as that. However BOOLEAN (*func)(struct vm*); needs to be changed to void (*func)(struct vm_node *); to match the destination function.

functions are sort of like arrays so you can just do

menu_item.func = print_list;

just like you would do with an array

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM