Brian
Brian

Reputation: 7326

UDP recvfrom warning with gcc compiler

I am receiving the following warning when compiling my client - server UDP socket simulation:

warning: passing 'int *' to parameter of type 'socklen_t *' (aka 'unsigned int *') converts between pointers to integer types with different sign [-Wpointer-sign]

This is the concerned code:

#define BUFLEN 2048
struct sockaddr_in myaddr, remaddr;
int fd, recvlen, slen=sizeof(remaddr);
...
char response[BUFLEN];
recvlen = recvfrom(fd, response, BUFLEN - 1, 0, (struct sockaddr *)&remaddr, &slen);

I have been referencing a tutorial from Rutgers university. I have a fair idea as to why this warning is occuring (it wants me to use socklen_t *), but I wanted to ask the SO community.

  1. Why is this warning occuring?
  2. How can I get rid of it?

Upvotes: 0

Views: 570

Answers (2)

John Bollinger
John Bollinger

Reputation: 180351

Why is this warning occuring?

The warning is occurring because socklen_t is an unsigned type, and int is a signed type (as the diagnostic says). In principle, therefore, the pointed-to value may be interpreted differently by recvfrom() than it is by the caller. In practice, such a difference won't happen with GCC-compiled code as long as only correct values of the length of the address struct are used, but GCC can't check that at compile time.

How can I get rid of it?

Pass an argument of the correct type.

Upvotes: 1

user207421
user207421

Reputation: 310957

it wants me to use socklen_t *

Correct.

Why is this warning occuring?

Because you aren't using socklen_t.

How can I get rid of it?

Declare slen as socklen_t.

Upvotes: 1

Related Questions