【问题标题】:accessing a member of a struct via pointer,results in error通过指针访问结构的成员,导致错误
【发布时间】:2013-05-25 01:07:37
【问题描述】:

这是我写的结构体。

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’

我对这些错误消息感到困惑。我是否以错误的方式赋值?

【问题讨论】:

  • 您确定您包含正确的 client_t 定义,即您没有其他不同的定义吗?
  • index 中删除=NULL。您还应该考虑是否需要一个指针数组(在这种情况下,您需要分配内存)或者您是否打算声明一个 client_t 实例数组 - static client_t clients[MAXCLIENTS];
  • 删除了 Null 并且问题消失了。谢谢

标签: c pointers error-handling struct


【解决方案1】:

结构中不允许赋值。尝试将索引分配给结构外的 NULL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    相关资源
    最近更新 更多