C Programming

C: Init_ntop function example

The init_ntop() function is a feature that is used to convert a 32 bit IPv4 and 128-bit IPv6 address into a readable format. Unlike the init_ptop function, the address is converted in the original form as it was converted into some binary numeric form by using init_pton(). It means these both functions are usable for privacy purposes for encryption and decryption of IP addresses.


#include <arpa/inet.h>
# const character *inet_ntop(int af, const void *source, character *dst, socklen_t size);

Just like init_pton, it has three main arguments as the parameter, but it also has a 4th argument that deals with the size of the socket/buffer that is pointed by “dst.” Here, we will describe the parameters in the init_ntop() function.


The general description of this function is that this function converts the IP address in the readable binary format. This is primarily a numeric form of simple text. This conversion is done through the specified “src,” which converts it in the text format, and then the resultant value is placed in the “dst.” It is necessary to check the size/ space of the dst (destination) area. Because if the place is sufficient to store the address, then the resultant address is placed. There needs to be a free buffer extension in the second case to add the address.

The argument “af” refers to the family of the internet address. It can be AF_INET for IPv4 by default or AF_INET6 for IPv6. The parameter arguing “src” shows the buffer holding the IPv4 internet address if the “af” argument is AF_INET or IPv6. The provided address must be in the order of network byte.

Just like the source argument, the destination ‘ds’ refers to the buffer, where the init_ntop() function will store the resultant address in the form of a string. The 4th one specifies the size argument pointing to the size of the buffer. It is stated for this argument that it should always specify a non-NULL argument for a destination. For IPv6 addresses, the storage buffer should have a minimum of 46 bytes, whereas, in the case of IPv4 addresses, the buffer should be at least 16 bytes.

To allot storage in the form of a buffer is necessary and should be done at the priority level because the size matters to store the address of the resultant value to avoid any size-related trouble. We have defined two constraints to allow applications to declare/ allot buffers easily of accurate size to take IPv4 and IPv6 addresses in the string format. These constraints are also defined in the library <netinet/in.h>.


Return value

The return type is the value obtained by the function in both cases; either it is successfully called or unsuccessfully terminated because of any error. But it always returns; that’s why we always use the ‘int’ return type for the main function. If the function is successful, inet_ntop() returns a pointer that is returned to the buffer that contains the address after the conversion process. On the other hand, if the function is unsuccessful, inet_ntop() returns a NULL or a ‘0’ and sends the errno to rectify the error easily.

Errors caused by the function init_ntop ()

Many possible errors can suppress the init_ntop() function to perform effectively, but we have highlighted mainly two of them here.


A parameter is an invalid number. In other words, it does not belong to a family of the supported network.


This error occurs because of less space to store the converted address. The destination “dst” is not large, which leads to sufficient storage to store the resultant value or translated address. So the errno mentions the reason, and then these errors are removed.

Just like inet_pton, init_ntop is also related to socket programming. Because the socket function contains the domain argument as a parameter that belongs to the AF_INET (IP) so in the case of both protocols, IPv4 or Ipv6 are selected.

Implementation of INIT_NTOP()

Before starting the implementation, we have seen that the usage and general description of this function are also shared on the manual page of the Ubuntu Linux operating system to facilitate the user with its working functionality.

$ man inet_ntop

Using the command mentioned above, the user is directed towards a page containing all the descriptions of init_ntop(). We have attached a snippet for your assistance.

Example 1

We have implemented the examples on the Linux operating system; for this purpose, you need to have a text editor to write source codes in it. Whereas for the resultant values, we will use the Ubuntu terminal. Open the Ubuntu default text editor and use the below-mentioned source code to demonstrate the working of init_ntop().

Init_ntop() works opposite to init_pton; if you have some know-how of init_pton(), you will easily understand the functionality. Otherwise, converting addresses is not too difficult using these functions in the C programming language.

Starting with the libraries, you can see that we have used an arpa/inet.h library, because it contains all the information regarding internet addresses. On the other hand, the system socket library also needs to be headed as the connection is not possible without it.

#include <sys/socket.h>
#include <arpa/inet.h>

After the libraries, we have used the constraints to mention addresses regarding internet protocols 4 and 6. The address provided here is a binary format converted into a numeric and easily understandable format. Two structures are initialized here for both th4 and 6 IPs. Similarly, both buffers are used here to store the resultant values. By calling the init_ntop function, one should make sure that the size of the buffer is not null. And then, after conversion, the address is displayed. In the other part, the error needs to be identified. A similar case is with SF_INET6.

The execution needs a compiler. This is a GCC compiler. With the compiler, the file name is mentioned. Her ‘ntop.c’ is the name of a file.

$ gcc –o ntop ntop.c
$./ ntop

On execution, you will see that both the addresses for both internet protocols are displayed successfully without encountering any error.

Example 2

This example involves the usage of both inet_ntop() and inet_pton() functions collectively in a single C source code. The pton() function contains three arguments with the address. At the same time, inet_ntop() has 4 parameters with the buffer size. First, pton() converts the address to binary format with numeric values not easily readable by humans. An init_ntop () converts it back into a text format.

Compile the code and execute it.

You can see that the address provided as an input is displayed without any change by using a simple string to create an address in the text format.


It is concluded that the article ‘init_ntop function example’ contains all the possible general descriptions regarding the usage of the ntop() function along with the arguments it uses. Some errors have also been mentioned that need to be identified if something goes wrong regarding storage space or the source destination. Anyhow, we have used two basic but impactful examples to demonstrate the working of this function.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

Page was generated in 0.061333894729614