2014-10-07 120 views
0
typedef struct 
{ 
    int i; 
}one; 

typedef struct 
{ 
    one two; 
}three; 

void writing_a_value(three *); 

int main() 
{ 
    three four; 
    writing_a_value(&four); 
    printf("%d",four.two.i); 
} 

void writing_a_value(three *four) 
{ 
    four->(two->i)=1; /* problem here */ 
} 

我已經嘗試了使用類似(four->(two->i))=1的大括號,但它仍然無法正常工作。我必須傳遞指針,因爲我必須將數據輸入到嵌套結構中。 error=expected (bracket,在註釋行中。通過嵌套結構傳遞函數使用指針

我怎樣才能使用指針傳遞結構並在嵌套結構中輸入數據?

+0

我編輯你的代碼因爲它不可讀。你收到什麼錯誤消息? – DOOM 2014-10-07 16:22:15

+2

0)'wriring_a_value' - >'writing_a_value' 1)'four - >(two-> i)= 1;' - >'four-> two.i = 1;'2)'printf(「%d 「,two.i);' - >'printf(」%d「,four.two.i);' – BLUEPIXY 2014-10-07 16:24:37

+2

你正在用一,二,三...命名來破壞自己。 – 2501 2014-10-07 16:53:20

回答

5

二是不是一個參考,所以試圖解引用它會導致錯誤。相反,你應該只解除四個。

void writing_a_value(three *four) 
{ 
     four->two.i=1; /*no problem here */ 
     //(*four).two.i=1 would accomplish the same thing 
}