c - 通过指针访问结构的成员,导致错误

标签 c pointers error-handling struct

这是我写的结构。

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

接下来我将制作这些结构的数组。

static client_t *clients[MAXCLIENTS];

现在在 main 函数中,我根据数组中的位置为这些结构分配值。

    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’

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

最佳答案

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

关于c - 通过指针访问结构的成员,导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16819539/

相关文章:

c - 带匹配括号的正则表达式

c++ - 如何通过名称获取 id 顶点?

c - 什么是双星(例如 NSError **)?

C++ 返回对新对象的引用

python - 有理由在python中用try try语句包装kwargs.get()吗?

c# - ASP.net 应用程序中的奇怪错误屏幕

c - 指针指向链表中错误的元素 (C)

c - 如何初始化所有元素都为0的二维数组?

c++ - 删除数组指针堆损坏

python - 如何捕捉python语法错误?