Converts an IPv4 network address structure to a string in standard dot-decimal notation.
char *inet_ntoa(
struct in_addr InAddress
);
NULL.
The inet_ntoa function converts an IPv4 address, stored in network byte order in an in_addr structure, to a character string in standard dot-decimal notation (e.g., "192.168.1.1").
The returned string is stored in a static buffer. This means that if you call inet_ntoa multiple times without saving the result, subsequent calls will overwrite the buffer from previous calls. For thread-safe operations, consider using InetNtop.
NULL to handle potential errors.| Component | Value |
|---|---|
| Minimum supported client | Windows 2000 Professional |
| Minimum supported server | Windows 2000 Server |
| Header file | Winsock2.h |
| Library | Ws2_32.lib |
This example demonstrates how to use inet_ntoa to convert a network address to its string representation.
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
int main() {
WSADATA wsaData;
struct in_addr addr;
char* ip_string;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
fprintf(stderr, "WSAStartup failed.\n");
return 1;
}
// Example IP address (localhost)
addr.s_addr = inet_addr("127.0.0.1");
// Convert the address to a string
ip_string = inet_ntoa(addr);
if (ip_string != NULL) {
printf("IP Address (dot-decimal notation): %s\n", ip_string);
} else {
fprintf(stderr, "inet_ntoa failed.\n");
}
WSACleanup();
return 0;
}