The inet_pton()
documentation:
...
SYNOPSIS
#include <arpa/inet.h>
const char *inet_ntop(int af, const void *restrict src,
char *restrict dst, socklen_t size);
...
DESCRIPTION
The inet_ntop()
function shall convert a numeric address into a text
string suitable for presentation. The af argument shall specify the
family of the address. This can be AF_INET
or AF_INET6
. 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. The dst
argument points to a buffer where the function stores the resulting
text string; it shall not be NULL
. The size argument specifies the
size of this buffer, which shall be large enough to hold the text
string (INET_ADDRSTRLEN
characters for IPv4, INET6_ADDRSTRLEN
characters for IPv6).
...
RETURN VALUE
The inet_ntop()
function shall return a pointer to the buffer
containing the text string if the conversion succeeds, and NULL
otherwise, and set errno
to indicate the error.
...
ERRORS
The inet_ntop()
and inet_pton()
functions shall fail if:
[EAFNOSUPPORT
] The af
argument is invalid.
[ENOSPC
] The size of the inet_ntop()
result buffer is inadequate.
And the definition of a "string":
3.92 Character String
A contiguous sequence of characters terminated by and including the
first null byte.
Given that, the answer to
- Null string termination: it is not clear whether I need to add a null string delimiter throughout the memory allocation where inet_ntop is printing when compiling at order 3 or more
should be clear. A string by definition includes the null byte. inet_pton()
returns a pointer to a buffer that contains such a string. No addition of a null byte is needed.
And the answer to
- I do not know whether the output of the function returns a pointer to the last written character, or the first written character.
is given by the C standard in 6.3.2.3 Pointers, paragraph 7:
... When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object.
So, the "pointer to the buffer containing the text string if the conversion succeeds" returned by inet_ntop()
points to the first byte in the string.