2011-09-09 72 views
-1

我想訪問結構中的結構,有誰知道如何?結構在struct c內

編輯:

typedef struct{ 
    int a, b; 
} struct_1; 

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

這裏有個例子我想.exemple.a

感謝您分配一個值表[0]。 編輯:哇即時通訊這樣的dumba ..有時它工作的IITs只是我的打印是打印100次,而我剛剛6項所以我不得不查找打印感謝上反正

+0

'Table [0] .exemple.a = value;'缺少什麼東西? –

+0

我剛剛編輯了問題 – Glove

回答

3

酷似你的例如:

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]; 
+0

哦,對不起,我只是編輯了例子 – Glove

0

對於嵌套的結構,你繼續下去,直到你找到你在找什麼,像這樣來訪問屬性:

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

我想你可能是指:

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,應該工作,例如。

0

您聲明的struct_2看起來不對。將struct_2 exemple;替換爲struct_1 exemple;。要訪問結構中的數據,如果使用指針,請使用.運算符或->運算符。