i have the following code in a function
char MenuOptions[7][200];
strcpy(MenuOptions[0], "Create New / Modify Existing Customer");
strcpy(MenuOptions[1], "Create New / Modify Existing Product");
strcpy(MenuOptions[2], "List All customers");
strcpy(MenuOptions[3], "List All Products");
strcpy(MenuOptions[4], "Batch Update of New Stock");
strcpy(MenuOptions[5], "Create Customer Order");
strcpy(MenuOptions[6], "View Last Order for Customer");
switch (displayMenu("Main Menu", MenuOptions, 7, TRUE)) {
etc....
and
char displayMenu(char *name, char *options[], int menuLength,
enBoolean QuitEnabled) {
int i;
printf("%s: \n", name);
for (i = 0; i < menuLength; i++) {
printf("\t %d %s.\n", (i + 1), options[i]);
}
if (QuitEnabled == TRUE)
printf("\t Q. Quit\n");
etc
once the displayMenu method is entered though, the MenuOptions array seems to be lost from memory. I tried seeing what options[i] is through the expression window in eclipse and it says that it’s “out of bounds” as a value
First and foremost, turn on warning options in your compiler. Your compiler should have warned you there was a type mismatch.
MenuOptionsis an array of seven arrays of 200char. WhendisplayMenuis called, andMenuOptionsis passed as a parameter,MenuOptionsis automatically converted from an array to a pointer to the first of the seven arrays of 200 char.In
displayMenu, theoptionsparameter is declared withchar *options[]. This means thatoptionsis an array of pointers tochar. In function parameters,[]is special; it suggests that an array will be passed for this parameter, but the array is actually passed by the address of its first argument. Thus, this declaration says thatoptionsis a pointer to the first of some number of pointers tochar.So, you are passing a pointer to the first of seven arrays of 200 char, but the function expects to receive a pointer to the first of some number of pointers to
char.One way to fix this is to change the declaration of
optionstochar (*options)[200]. This says thatoptionsis a pointer to (the first of) arrays of 200 char.A better way to fix it may be to change MenuOptions:
(You do not need “7” in the brackets because the compiler will count the strings for you.)
Then you do not need to change the declaration of
options, except that I have addedconst:Now:
MenuOptionsis an array of seven pointers to strings (arrays of char). It is passed as a pointer to pointers to strings, andoptionsis a pointer to pointers to strings, so the types match.MenuOptionsdoes not take more space than required. Before, it was defined with 200 characters for each string. Now, each string takes only as much space as it needs (plus a null character at the end and a pointer to the string in theMenuOptionsarray).constis added to avoid accidentally changing the strings.MenuOptionsis declaredstatic, so that it is initialized at compile time, and you do not need to copy strings into it at run time.Finally, you can replace the “7” in the call to
displayMenuwithsizeof MenuOptions / sizeof *MenuOptions. This divides the size of the array by the size of an element of the array, giving the number of elements in the array. This is preferred over hard-coding a constant because it eliminates the possibility that somebody might change the size of the array without changing the hard-coded constant.