0

I'm trying to initialize an array which will be a menu for my program. I want it to save the name of the option and a pointer to a function that will be called when the user selects the corresponding option.

I'm only new to C and I'm experiencing a few issues with my current setup which looks like this -

void init_menu(struct menu_item *menu)
{ 
    /* Array of function pointers */
    #if 0
        BOOLEAN (*funcs)[NUMOPTIONS](struct ppd_system)
    #endif
    function funcs[NUMOPTIONS] =
    {
        display_items, purchase_item, save_system, 
        add_item, remove_item, reset_stock, reset_coins,
        display_coins,
    };

    /* Array of Strings */
    const char *name[NUMOPTIONS] =
    {
        "Display Items",
        "Purchase Items",
        "Save and Exit",
        "Add Item",
        "Remove Item",
        "Display Coins",
        "Reset Stock",
        "Reset Coins",
        "Abort Program"
    };
}

My understanding is that my first array should consist of pointers to those functions, and my second array should have the name that corresponds with the function.

The problem is all the functions are returning "undeclared identifier" as they're declared in a different file (options.c, while this one is menu.c). Is there some way to pass all the options through so that they can be used in this file? (I can't change parameters as this is a homework task)

Thanks for your time.

This might be helpfull as well:

struct menu_item
{
    /* the text to be displayed in the menu */
    char name[MENU_NAME_LEN + 1];
    /* pointer to the function to be called when this item is selected */
    BOOLEAN (*function)(struct ppd_system*);
};

typedef BOOLEAN (*function)(struct ppd_system*);
Peter Varo
  • 11,726
  • 7
  • 55
  • 77
Ryan
  • 33
  • 1
  • 6
  • have you written a separate header file, where all your function prototypes are in, and have you inlcuded that to this file? – Peter Varo Oct 07 '15 at 13:11
  • Hi Peter, Thanks for your time, I have done that one. – Ryan Oct 07 '15 at 13:12
  • what is the exact compiler command (with all the flags) your are using? I mean, are you defining the include path, so that the compiler can find your header files? – Peter Varo Oct 07 '15 at 13:13
  • gcc -ansi -Wall -pedantic *.c -o test (There is about 10-12 files for this program) I #include my main in this file, inside main i #include the others – Ryan Oct 07 '15 at 13:14
  • where are the `-I` flags to define the include path for the CPP? (personal suggestion, also use: `-Wextra -v` -- but this is not strictly connected to the issue, just a suggestion) – Peter Varo Oct 07 '15 at 13:15
  • I'm not sure what CPP is im only new to C programming – Ryan Oct 07 '15 at 13:16
  • CPP => C Pre Processor – Peter Varo Oct 07 '15 at 13:17
  • @Ryan You mean like this ==>> http://stackoverflow.com/questions/32614150/clarification-on-function-pointers-in-c/32615240#32615240 ? – Michi Oct 07 '15 at 13:26
  • @Michi I believe that mine is basically doing the same thing as this example however all my functions are "undeclared identifier 'function'" when i try to run it – Ryan Oct 07 '15 at 13:31
  • the declared arrays are all hidden inside the function: `init_menu()`. I do not see any code to display the menu, I do not see any code to retrieve a selection from the user. I do not see any code to dispatch a specific selection handler function after the user has entered a selection. What is the definition of 'function'? What is the definition of 'BOOLEAN'? Do you understand that the array 'name' is actually just a series of pointers to the literals listed? What is the definition of 'NUMOPTIONS'? I.E. the question needs a lot more details/code for us to answer. – user3629249 Oct 07 '15 at 13:49
  • the file that contains `init_menu()` needs to #include the header file(s) for the listed functions. – user3629249 Oct 07 '15 at 13:53

0 Answers0