的數組如何聲明三指針的指針數組聲明三重指針像我有如何指針
char *mainmenu[] = {"menu1", "menu2", "menu3"}
看到的畫面
alt text http://www.freeimagehosting.net/uploads/6988d25305.png
如何連接我的菜單1, 2,3與那些從圖片m1p1 m2p1 ???我只需要語法請幫我...
的數組如何聲明三指針的指針數組聲明三重指針像我有如何指針
char *mainmenu[] = {"menu1", "menu2", "menu3"}
看到的畫面
alt text http://www.freeimagehosting.net/uploads/6988d25305.png
如何連接我的菜單1, 2,3與那些從圖片m1p1 m2p1 ???我只需要語法請幫我...
您可以使用多個*
或多個括號組。鑑於你描述的數據結構,我會去
char *mainmenu[X][Y] = {{"m1p1", "m1p2", "m1p3"}, {"m2p1", "m2p2"}}
。
請注意,必須定義Y.在C中的多維數組中,必須定義除最外層維以外的所有維的長度(如果使用數據初始化它)。
all[0]
是char **
類型,並且將匹配的的mainmenu
定義,儘管它似乎與陣列中一個終止NULL
。
char ***all;
char *mainmenu[] = {"menu1", "menu2", "menu3", NULL};
all[0] = mainmenu;
使用未初始化的局部變量'all':S – ScReYm0 2010-07-26 10:53:35
這是您從圖片中涉及的類型的模型,而不是逐字解決方案。如果這是你需要的,請參考關於數據結構的C編程教程! – 2010-07-26 15:41:43
它更然後你問,但應該是有幫助的:
/* Functions associated to menu items */
void M1P1() { puts("Hey! You selected menu 1 position 1!"); }
void M1P2() { puts("Hey! You selected menu 1 position 2!"); }
void M1P3() { puts("Hey! You selected menu 1 position 3!"); }
void M2P1() { puts("Hey! You selected menu 2 position 1!"); }
void M2P2() { puts("Hey! You selected menu 2 position 2!"); }
// ...
/* structure describing single sub-menu item */
typedef struct {
char *caption; // item caption
void (*action)(); // function associated to this item
} SubMenuItem;
/* array of all sub-menu items of menu1 */
SubMenuItem sub_menu1[] = {
{ "m1p1", M1P1 },
{ "m1p2", M1P2 },
{ "m1p3", M1P3 },
};
/* array of all sub-menu items of menu2 */
SubMenuItem sub_menu2[] = {
{ "m2p1", M2P1 },
{ "m2p2", M2P2 },
};
// ...
/* structure describing single main-menu item */
typedef struct {
char *caption; // item caption
SubMenuItem *sub_menus; // array of sub-menu items
unsigned sub_menus_count; // number of sub-menu items (length of the array)
} MenuItem;
/* array of all main-menu items */
MenuItem menu[] = {
{ "menu1", sub_menu1, sizeof(sub_menu1)/sizeof(sub_menu1[0]) },
{ "menu2", sub_menu2, sizeof(sub_menu2)/sizeof(sub_menu2[0]) },
// ...
};
/* number all main-menu items */
#define MENU_ITEMS_COUNT (sizeof(menu)/sizeof(menu[0]));
/* Example - iterationg menu */
int i, j;
for (i = 0; i < MENU_ITEMS_COUNT; i++) { // iterate through main-menu items
printf("%d) %s\n", i + 1, menu[i].caption); // print main-menu item index and caption
for (j = 0; j < menu[i].sub_menus_count; j++) { // iterate through sub-menu items of current main-menu item
printf("\t%d.%d) %s\n", i + 1, j + 1, menu[i].sub_menus[j].caption); // print indices and sub-menu item caption
}
}
putchar('\n');
/* Example - running action associated to menu item */
/* To run action associeted to menu 1 position 2 */
menu[0].sub_menus[1].action();
可以通過嵌套數組,但你需要明確指定所有維度安全可靠。 – sbi 2010-07-26 10:39:06