Joshua Theeuf
Joshua Theeuf

Reputation: 27

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

Upvotes: 1

Views: 82

Answers (2)

Farouq Jouti
Farouq Jouti

Reputation: 1667

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 /*

Upvotes: 0

Krypton
Krypton

Reputation: 3325

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.

Upvotes: 1

Related Questions