So what I have is a file main.c which is including a header file I made utils.h, containing forward references to functions in my source file utils.c
In utils.c:
I have a function that accepts an array of string as an argument, and prints it out as a menu:
void showMenu(const char *menu[])
{
int menulen = sizeof(menu)/sizeof(*menu);
int i;
for(i = 0; i < menulen; i++)
{
printf("[%d] .. %s\n", (i+1), menu[i]);
}
}
In main.c:
I simply call this function:
const char *menu[] =
{
"Customers",
"Orders",
"Products"
};
int main(void)
{
showTitle("Customer Orders System");
int menulen = sizeof(menu)/sizeof(*menu);
showMenu(menu);
getch();
}
My Problem:
My showMenu function is calculating the length of the array, and then iterating through it, printing the strings. This used to work when the function was in main.c, but I am required to organize this project in separate files.
The length is now being calculated as 1. After doing some debugging, I think this is a pointer-related problem, but I seem to resolve it. The argument for showMenu after the call is of type
const char** menu
having only the first element of my original array.
I tried deferencing the argument, passing it a pointer of the array, and both at the same time.
Strangely enough, the same line of code works in the main function.
I really don't want to have to resolve this problem by adding a length of array argument to the function.
Any help is greatly appreciated.