0
votes

any help would be appreciated. Im developing a udp connection and i have a function that initialize the parameters of an address. here is the code

void Socket::InitAddrInfoStruct (struct addrinfo *hints, bool socktype){

/* Setting addrinfo struct */
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
if(socktype == true)
    hints.ai_socktype = SOCK_DGRAM; // set to SOCK_DGRAM to force UDP protocol type
else
    hints.ai_socktype = SOCK_STREAM; // set to SOCK_STREAM to force TCP protocol type
hints.ai_flags = AI_PASSIVE; // use my IP

}

when i try to compile i got the following error: request for member ‘ai_family’ in ‘hints’, which is of non-class type ‘addrinfo*’

can anyone help me on how i should add data to the struct hints?

3

3 Answers

1
votes

The error is saying that hints is not of class type, and indeed its a pointer. You should access its members with -> instead:

hints->ai_family = AF_UNSPEC;

or alternatively:

(*hints).ai_family = AF_UNSPEC;

Also, as @Anders K. pointed out, your memset is probably wrong since you are invoking it with the size of a pointer. It should be:

 memset(hints, 0, sizeof(*hints) );

or alternatively:

 memset(hints, 0, sizeof(struct addrinfo) );
1
votes

This is not directly part of your question, but the memset is not correct. It should probably be of the form:

memset(hints, 0, sizeof(struct addrinfo));

In response to the comment: In this case the variable is a pointer, so sizeof(hints) would be 4 bytes (or 8 bytes on a 64-bit system). It could also be written sizeof(*hints) (essentially saying the size of the structure pointed to by the variable). So the distinction in this case is whether you are referring to the structure itself or a pointer to the structure.

In reality, the memset call in the OP is "consistent". It is setting the contents of the variable hints to 0 and it passed the address of the variable &hints to the memset function and the size of that variable as the number of bytes to memset. So the call was "consistent" and "correct" but the result is that it would clear the variable and the very next statement (hints->ai_family) would be dereferencing a null pointer. So while "correct", it was probably not the intention.

0
votes

sounds like you are missing a header

have you included the header with addrinfo struct? in windows its Ws2tcpip.h