0

From the "man" pages it looks like inet_ntop returns a string (const char*) which should be alright when comparing to NULL. However, in my program I get a compiler warning at the first line in this code block that says:

warning: comparison between pointer and integer

. Assuming the correct parameters are being passed in, is there something wrong with this:

    if(inet_ntop(address->sa_family, numericAddress, addrBuffer, sizeof(addrBuffer)) == NULL)
    {
        fputs("invalid address", stream);
    }

Unfortunately I can't distinguish which one it sees as a pointer and which one it sees as an integer. Thanks in advance!

Matt Vaughan
  • 1,135
  • 2
  • 13
  • 19

1 Answers1

2

Have you included the "arpa/inet.h" file?

The "arpa/inet.h" contains inet_ntop declarations. If you do not include it, the compiler will assue inet_ntop returns an int.

X Zhang
  • 197
  • 1
  • 6
  • Why wouldn't it issue a function not declared or something? – Matt Vaughan May 12 '13 at 03:37
  • 1
    @MattVaughan In C, if the compiler find an undeclared function it assumes it returns `int`. As you compare the returned value with a pointer (`NULL` is defined to be a `void *`) you get the warning. And of course there will be a mismatch with the function arguments as well, which can cause undefined behavior. – Some programmer dude May 12 '13 at 03:41
  • 1
    The C language (at least C89) has the concept of implicit function declarations, where a function without a prototype has the return type and arg list derived from its first use in a function call, and lacking that, it returns int and takes an unspecified but fixed number of arguments. You can add -Wmissing-prototypes in your compiler options. – X Zhang May 12 '13 at 03:41
  • Very helpful, thank you! I will have to see what header I am missing. – Matt Vaughan May 12 '13 at 03:46
  • @MattVaughan Always compile with `-Wall -Wextra`, that would tell you about such and many more things. – Daniel Fischer May 12 '13 at 10:05