0

in socket programming,

int inet_pton(int af, const char *src, void *dst);

in this function why destination address is of void type pointer .why not int or char pointer? And also how to handle this void type pointer. i am accessing in this way,is it correct

void *buf;
int a;
inet_pton(AF_INET,ip,buf);
printf("%s",(char*)buf);

1 Answers1

0

This function converts textual representation of a network address to either IPv4 or IPv6 binary address, so that dst points to either in_addr or in6_addr.

This is to support the paradigm that the networking layer in an application can be generic and handle both IPv4 and IPv6 protocols simultaneously. Such an application would store the network addresses in a storage of size sufficient for both in_addr or in6_addr, along with the address family, e.g.:

struct generic_in_addr
{
    int af;
    union {
        in_addr v4;
        in6_addr v6;
    } addr;
};

And would use the same multi-protocol functions such as socket, connect, accept passing the network address along with its family and size.

Generic network programming in C.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271