简体   繁体   中英

accessing a member of a struct via pointer,results in error

This is a struct I have written.

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

Next I am making an array of these structs.

static client_t *clients[MAXCLIENTS];

Now in the main function, I am assigning values for these structs according to the position in the array.

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

when i compile, i get these error messages.

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’

I am confused about these error messages. Have I assigned values in a wrong way?

Assignments are not allowed in a struct. Try assigning index to NULL outside the struct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM