[英]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;
我覺得你的問題是, exemple
是struct_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.