简体   繁体   English

如何在指针数组中声明三指针

[英]How to declare triple pointers in array of pointers

How to declare a triple pointer with array of pointers like i have 如何使用像我一样的指针数组声明一个三重指针

char *mainmenu[] = {"menu1", "menu2", "menu3"}

see the picture 看到图片

替代文字

How to connect my menu1,2,3 with those from the picture m1p1 m2p1 ??? 如何连接我的menu1,2,3与图片m1p1 m2p1 ??? I just need the syntax please help me ... 我只需要语法请帮帮我...

all[0] is of type char ** , and will match your definition of mainmenu , albeit it appears with a terminating NULL in the array. all[0]的类型为char ** ,并且将匹配您对mainmenu的定义,尽管它在数组中显示为终止NULL

char ***all;
char *mainmenu[] = {"menu1", "menu2", "menu3", NULL};
all[0] = mainmenu;

You can use more than one * , or more than one set of brackets. 您可以使用多个*或多个括号。 Given the data structure you described, I'd go with 鉴于您描述的数据结构,我会选择
char *mainmenu[X][Y] = {{"m1p1", "m1p2", "m1p3"}, {"m2p1", "m2p2"}} . char *mainmenu[X][Y] = {{"m1p1", "m1p2", "m1p3"}, {"m2p1", "m2p2"}}
Note that Y must be defined. 请注意,必须定义Y. In multidimensional arrays in C, you have to define the length of all but the outermost dimension (if you initialize it with data). 在C中的多维数组中,您必须定义除最外层维度之外的所有维度的长度(如果使用数据初始化它)。

It's more then you ask, but should be helpful: 这比你提出的更多,但应该有所帮助:

/* 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM