繁体   English   中英

C结构数组结构数组

[英]C Array of Structures of array of structures

在进行我的项目时,应该使用相当复杂的结构,这让我感到很困惑。 我需要声明一个结构数组(菜单),每个包含一个结构数组(元素)。 在我所来自的Visual Basic中,它将被(简化):

type t_element
  elementID as long
  size as long
  color as long
end type

Type t_Menu
  menuID as long
  numElements as long
  elements() as t_element 
end type

Dim Menus(10) as t_menu
Redim Menus(1).elements(5)

我在XC8中尝试了类似的方法:

typedef struct t_element
{
    char eleID;
    int size;
    int color; 
}Element;

typedef struct t_Menu
{
    char IDmenu;
    char elementNumber;
    Element elements[];
}Menus;

希望这是可行的,如何在Menus中声明Element的数组?

这不起作用:

Menus Menu[10];   //this is OK
Element Menu[1].elements[5];  //this is not
Menu[1].(Element)elements[5]; //this is not

有解决办法吗? 如果是,我在做什么错?

请参阅对结构元素的访问:

typedef struct t_element
{
    char eleID;
    int size;
    int color; 
}Element;

typedef struct t_Menu
{
    char IDmenu;
    char elementNumber;
    Element elements[100];
}Menus;

int main() 
{
    Menus menus[20];
    Element element1; 

    menus[5].elements[3].color = 1;
    menus[5].elements[3].size = 2;
    menus[5].elements[3].eleID ='a';
    menus[5].elementNumber = 1;

    element1 = menus[5].elements[3];
    int color = element1.color;  

   printf("color for menus[5].elements[3]=%d",color); 

   return 0;
}

暂无
暂无

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

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