2013-05-29 181 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。