I used to think the second argument for inet_ntop should always be a struct in_addr or struct in6_addr. But then I looked up the POSIX definition:
const char *inet_ntop(int af, const void *restrict src,
char *restrict dst, socklen_t size);
[…] The src argument points to a buffer holding an IPv4 address if the af argument is AF_INET, or an IPv6 address if the af argument is AF_INET6; the address must be in network byte order. […]
As you can see both the function prototype and the description are vague.
Why is this? And what are allowed/portable choices for src?
It’s a pointer to an IPv4 or IPv6 as stored in the respective headers – so a 4 byte buffer in the case of IPv4, and a 16 byte buffer in the case of IPv6.
struct in_addrandstruct in6_addrare convenient structures for storing such addresses, but you could useunsigned char [4]andunsigned char [16]respectively, if you wanted.