2013-05-29 169 views
3

這是我寫的一個結構。我想製作這些結構的數組。通過指針訪問結構的成員,導致錯誤

static client_t *clients[MAXCLIENTS]; 

現在在主函數中,我根據數組中的位置爲這些結構賦值。

clients[freeslot]->index=freeslot; 
    clients[freeslot]->sd=connfd; 
    clients[freeslot]->tid=syscall(SYS_gettid); 
    clients[freeslot]->name=threadnames[freeslot]; 

當我編譯時,我得到這些錯誤消息。

code.c:185:12: error: ‘client_t’ has no member named ‘index’ 
code.c:186:19: error: ‘client_t’ has no member named ‘sd’ 
code.c:187:19: error: ‘client_t’ has no member named ‘tid’ 
code.c:188:19: error: ‘client_t’ has no member named ‘name’ 

我對這些錯誤信息感到困惑。我是否以錯誤的方式分配了值?

+1

你確定你正在包含client_t的正確定義,即你沒有任何更多的不同定義嗎? – slugonamission

+2

從'index'刪除'= NULL'。你還應該考慮是否需要一個指針數組(在這種情況下,你需要分配內存),還是你想聲明一個'client_t'實例數組 - 'static client_t clients [MAXCLIENTS];' – simonc

+0

Removed the null問題消失了。感謝名單 – DesirePRG

回答

2

結構中不允許賦值。嘗試在結構外部將索引分配給NULL。