Reputation: 7799
I know it's not the first thread with this error but I really can't get what's wrong... My code is bellow:
/* File descriptor sets */
struct descs {
fd_set read;
fd_set write;
fd_set except;
};
typedef struct descs descs_t;
/* Server representation */
struct server {
int sock; /* Server sock descriptor */
struct sockaddr_in addr; /* Server address */
socklen_t addrlen; /* Server address size */
char buffer[BUFFER_SIZE]; /* Message buffer */
int len; /* Message buffer length */
};
typedef struct server server_t;
void build_fds(struct server *srv, struct descs *fd) {
FD_ZERO(fd->read);
FD_SET(STDIN_FILENO, fd->read);
FD_SET(srv->sock, fd->read);
FD_ZERO(fd->write);
// there is smth to send, set up write_fd for server socket
if (srv->len > 0) {
FD_SET(srv->sock, fd->write);
}
FD_ZERO(fd->except);
FD_SET(STDIN_FILENO, fd->except);
FD_SET(srv->sock, fd->except);
}
I am passing my struct via pointer so I must use ->
but it crashes with errors:
chatcl.c: In function ‘build_fds’:
chatcl.c:37: error: invalid type argument of ‘->’ (have ‘fd_set’)
chatcl.c:38: error: invalid type argument of ‘->’ (have ‘fd_set’)
chatcl.c:39: error: invalid type argument of ‘->’ (have ‘fd_set’)
chatcl.c:41: error: invalid type argument of ‘->’ (have ‘fd_set’)
chatcl.c:44: error: invalid type argument of ‘->’ (have ‘fd_set’)
chatcl.c:47: error: invalid type argument of ‘->’ (have ‘fd_set’)
chatcl.c:48: error: invalid type argument of ‘->’ (have ‘fd_set’)
chatcl.c:49: error: invalid type argument of ‘->’ (have ‘fd_set’)
make: *** [chatcl] Error 1
What's wrong in my code?
Upvotes: 1
Views: 924
Reputation: 5962
You are missing the &
, i.e. you need the address of the data structure, e.g.
#include <sys/select.h>
struct descs {
fd_set a;
};
void func(struct descs *d) {
FD_ZERO(&d->a);
}
Upvotes: 3
Reputation: 141235
From man select the FD_* macros they take pointers to fd_set
:
void FD_CLR(int fd, fd_set *set);
int FD_ISSET(int fd, fd_set *set);
void FD_SET(int fd, fd_set *set);
void FD_ZERO(fd_set *set);
You should pass pointers to the functions (macros), not values. You can use the address-of operator:
FD_ZERO(&fd->read);
FD_SET(STDIN_FILENO, &fd->read);
FD_SET(srv->sock, &fd->read);
and so on.
Upvotes: 2