Search code examples
cnetwork-programminginet-aton

Printing the return value from inet_aton() function


I have this code which is working fine:

#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    struct in_addr addr;

    if (argc != 2) {
        fprintf(stderr, "%s <dotted-address>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    if (inet_aton(argv[1], &addr) == 0) {
        perror("inet_aton");
        exit(EXIT_FAILURE);
    }

    printf("%s\n", inet_ntoa(addr));
    exit(EXIT_SUCCESS);
}

What I want to achieve is print the value of inet_aton() function. The description of function says that it returns a number, but when I try to print it, it says "cannot covert from address structure to decimal".


Solution

  • Using this instead of your last printf worked for me:

    printf("%d\n", addr.s_addr);