Joshua Theeuf Joshua Theeuf - 8 hours ago 2
C Question

How to define a function pointer in the current context?

Hey I am a bit confused as to how I would define a function pointer if I am given the following defs:

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


The way I have defined what initial variables that menu_item contains using the following using the function menu_init. This function is suppose to define what each member of the array of menu items.

void menu_init(struct menu_item * menu)
strcpy(menu[0].name, "\t1. Display Items");
menu[0].func = (*void)&print_list(struct vm_node node); //print list is suppose to print the entire linked list past to the function print_list. Though i am unsure if this is correct.

strcpy(menu[1].name,"\t2. Purchase Items");
strcpy(menu[2].name,"\t3. Save and Exit");
strcpy(menu[3].name, "\t4. Add Item");
strcpy(menu[4].name,"\t5. Remove Item");
strcpy(menu[5].name,"\t6. Display Coins");
strcpy(menu[6].name, "\t7. Reset Stock");
strcpy(menu[7].name,"\t8. Reset Coins");
strcpy(menu[8].name,"\t9. Abort Program");

/* The UNUSED() function is designed to prevent warnings while your
* code is only partially complete. Delete this function call once
* you are using vm in your own code */

The declaration of print_list is as follows.

void print_list(struct vm_node *root);

Any help would be much appreciated.


This is an example of creating and assigning (in the same line...) a function pointer

int g(int y){return y;}

int main()
    int (*f)(int x) = &g; // initializing f with the address of g. f is a pointer to a function that returns int, and has 1 int parameter
    printf("%d",f(1)); // calling g, using the pointer f
    return 0;