I am writing a network program where, in the server part, I want to accept connections from multiple clients using a listening socket. So I declare an array of address structs like this:
struct sockaddr_in* client;
which I create using malloc and later on, to accept connections I type:
newsock = accept(fd_skt, (struct sockaddr *)&client[i], &(sizeof(client[i])));
and there I get "lvalue required as unary '&' operand" from the compiler. Can anyone figure out what I have done wrong?
Yes, you can’t take the address of something that isn’t an lvalue, that is an object with an address. The result of the
sizeofoperator is just a value, it isn’t an object with an address.You need to create a local variable so that you can take its address.
E.g.
As an aside,
struct sockaddr_in* client;declares a pointer, not an array. To useclientas an array you need to assign it to a dynamically allocated array at some point before the call toaccept. I assume that this is what you are doing when you say “I create using malloc”.Alternatively you could actually declare
clientas an array.