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
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]);
}
}
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 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.
To pass an array as an argument to a method, you just have to pass the name of the array without square brackets. The method prototype should match to accept the argument of the array type. Given below is the method prototype: void method_name (int [] array);
Just like normal variables, Arrays can also be passed to a function as an argument, but in C/C++ whenever we pass an array as a function argument then it is always treated as a pointer by a function.
Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements.
To pass a one dimensional string to a function as an argument we just write the name of the string array variable. In the following example we have a string array variable message and it is passed to the displayString function.
This is because arrays decay into pointers to their first element when passed to a function like yours, and there is no information retained about the number of elements. In the scope where the array is declared, this decay hasn't happened, so sizeof
works.
You must either add length of array as an extra argument, or make sure the array is terminated by an appropriate sentinel value. One popular such value is NULL
, i.e. you make sure the last valid index holds a string pointer whose value is NULL
, which then indicates "no more data, stop":
const char *menu[] =
{
"Customers",
"Orders",
"Products",
NULL /* This is a sentinel, to mark the end of the array. */
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With