Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C : Pass Array of string as function argument

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.

like image 678
Zyyk Savvins Avatar asked Nov 27 '12 11:11

Zyyk Savvins


People also ask

How do you pass an array of strings as an argument?

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);

Can we pass array as argument in C?

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.

Can you pass an array as an argument?

Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements.

How do you pass a string as a function argument?

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.


1 Answers

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. */
};
like image 175
unwind Avatar answered Sep 20 '22 20:09

unwind