通过指针访问结构体成员会导致错误。

3

这是我编写的一个结构体。

typedef struct {
    int index=NULL;
    int sd;
    pthread_t tid;
    char* name;
}client_t;

下一步,我正在创建这些结构体的数组。
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
去掉了 Null,问题就消失了。谢谢。 - DesirePRG
1个回答

2

结构体中不允许赋值操作。请在结构体外将索引赋值为NULL。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接