char array pointer & functions

Hello All,

I have following issue with pointer:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char *menu_pages[] = {
                        "Add",
                        "Edit",
                        "Remove",
                        "Back",
                        (char *)NULL,
}

int create_menu(char *choice[])
{
      int n_choices;
      n_choices = (sizeof(choice) / sizeof(choice[0]));

}

int main()
{
      create_menu(menu_pages);
}





n_choices = 1
what is wrong with this?


thankx for ur help
-mm-
Last edited on
That won't work because the parameter choice is a char** which is a different type to the array menu_pages. Taking sizeof() an array gives you the amount of memory the whole array occupies. But taking the sizeof() a char** only tells you how big a char** is, not all the data it points to.

The problem is that the declarations look the same. But they are not. With the variable menu_pages, it has a type array of char* and the compiler knows how big the data it contains is. But with your parameter choice the compiler has no idea how much data it points to.
Taking sizeof() an array gives you the amount of memory the whole array occupies.

- this is what I need also, because I alocating memory for array (size may change) and alocating working fine :)

Besides of this, I need know how much items are in the menu_pages, so i use

while(choice[i]) i++;

working fine for me :)

thanks
Pass the size to the function.
Topic archived. No new replies allowed.