简体   繁体   English

C结构数组结构数组

[英]C Array of Structures of array of structures

Proceeding with my project which should use a quite complex structures, I felt in a dighole. 在进行我的项目时,应该使用相当复杂的结构,这让我感到很困惑。 I need to declare an array of structures (Menus) each one contains an array of structures (Elements). 我需要声明一个结构数组(菜单),每个包含一个结构数组(元素)。 In Visual Basic, from which I come, it will be (simplified): 在我所来自的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)

I tried something similar in XC8 with: 我在XC8中尝试了类似的方法:

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

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

Hoping that this is feasable, how can I declare the array of Element inside the Menus ? 希望这是可行的,如何在Menus中声明Element的数组?

This does not work: 这不起作用:

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

There is a solution? 有解决办法吗? If yes, what am I doing wrong ? 如果是,我在做什么错?

See the access to the elements of the structures: 请参阅对结构元素的访问:

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