繁体   English   中英

c中的struct内的struct

[英]struct within struct in c

我想访问结构中的结构,有人知道吗?

编辑:

typedef struct{
    int a, b;
} struct_1;

typedef struct{
    int c;
    struct_1 exemple;
} struct_2;
struct_2 Table[100];

例如,在这里我想为Table [0] .exemple.a分配一个值

谢谢。 编辑:哇我真是傻瓜..有时它的工作原理是我的印刷品正在打印100次,而我只有6个条目,所以无论如何我都不得不抬头看看印刷品

完全像您的示例:

Table[0].exemple.a = 12;

我觉得你的问题是, exemplestruct_2在你的榜样,而不是struct_1像它看起来你打算。 试一下以获取大小(拼写正确):

typedef struct{
  int a, b;
} struct_1;

typedef struct{
  int c;
  struct_1 example;
} struct_2;
struct_1 Table[100];

使用嵌套结构,您可以继续访问属性,直到达到所需的内容为止,如下所示:

Table[0].example.a = 5;
Table[0].example.b = 10;

我想您可能是说:

typedef struct{
    int c;
    struct_1 exemple; /* see how it's struct_1 */
} struct_2;

并不是

typedef struct{
    int c;
    struct_2 exemple;
} struct_2;

由于struct_2没有的a领域。

此后,例如Table[0].exemple.a = 5应该起作用。

您的struct_2声明看起来不正确。 替换struct_2 exemple; struct_1 exemple; 要访问结构内部的数据,请使用. 运算符或->运算符(如果使用指针)。

暂无
暂无

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

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