Reputation: 33
struct Users{
int id;
char msg[];
};
int nUsers;
struct Users users[10];
void connectUser(struct Users user){
if(nUsers<10){
for(int i=0;i<10;i++){
if(users[i]==NULL){
users[i]=user;
printf("user %d connected!\n", user.id);
nUsers++;
}
}
}else
printf("number of users reached!\n");
}
That's my code and when I try to compile, comes with error:
[s3450124@csitprdap01 ~]$ gcc -std=c99 socketserver.c -o socketserver
socketserver.c: In function ‘connectUser’:
socketserver.c:24: error: invalid operands to binary == (have ‘struct Users’ and ‘void *’)
socketserver.c:21: note: The ABI of passing struct with a flexible array member has changed in GCC 4.4
socketserver.c: In function ‘disconnectUser’:
socketserver.c:37: error: incompatible types when assigning to type ‘struct Users’ from type ‘void *’
Every time I try to compile, these errors comes up. Can you guys help me?
Upvotes: 1
Views: 292
Reputation: 9817
As noticed by @CoolGuy, the problem is due to if(users[i]==NULL)
. Since users
is an array of struct Users
, users[i]
is a struct Users
and a struct Users
cannot be compared to NULL
.
A pointer to a struct Users
, declared as a struct Users*
can be compared to NULL
.
Now that the problem is found, how to solve it ?
The straightforward answer is to use a convention. For instance, let's say that users[i].id==0
means that this user is disconnected. At the beginning of the program, the users must be initialized :
for(i=0;i<10;i++){
users[i].id=0;
}
As the user is connected a free slot must be found :
for(int i=0;i<10;i++){
if(users[i].id==0){
users[i]=user;
printf("user %d connected!\n", user.id);
nUsers++;
break;
}
}
Do not forget the break
statement : the user must be connected only once !
As the user is diconnected, users[i].id=0;
The other option is to declare struct Users *users[10];
. Hence users
is an array of pointers to struct Users
. Again, these pointers must be initialized :
for(i=0;i<10;i++){
users[i]=NULL;
}
As a new user is connected, some memory must be allocated or a valid pointer must be provided.
for(int i=0;i<10;i++){
if(users[i]==NULL){
users[i]=malloc(1*sizeof(struct user));
if(users[i]==NULL){printf("malloc failed\n");exit(1);}
users[i]->id=user.id;
printf("user %d connected!\n", user.id);
nUsers++;
break;
}
}
Using users[i]=&user;
would not be a good idea, because user
is a local variable : it does not exist out of the function connectUser(struct Users user)
. If you do so, it can trigger an undefined behavior somewhere else.
As the user is disconnected, the memory must be freed and the pointer must be set to NULL
: free(users[i]);users[i]=NULL;
Upvotes: 1
Reputation: 9708
The type of users[i] when say i = 0 is Struct Users. It is NOT a pointer so cannot have a value of NULL. That is what your compiler is complaining about. you are checking if the value is NULL when it cannot be. You only check for NULL when comparing pointers.
// you already check here that you do not exceed bounds of array - 10
for(int i=0;i<10;i++){
users[i]=user;
etc
Upvotes: 3