标题：inet_ntoa()
函数简介
linux下：

程序示例
程序示例1：
下面这个例子来自MSDN：[1]
#include <winsock2.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib, "wininet.lib")
// link with Ws2_32.lib
int __cdecl main(int argc, char **argv)
{
//-----------------------------------------
// Declare and initialize variables
WSADATA wsaData;
int iResult;
unsigned long ulAddr = INADDR_NONE;
// Validate the parameters
if (argc != 2) {
printf("usage: %s <IPv4 address>\n", argv[0]);
printf("  inetaddr converts a string containing an\n");
printf("  IPv4 address in one of the supported formats\n");
printf("  to a unsigned long representing an IN_ADDR\n");
printf("      %s 192.168.16.34\n", argv[0]);
return 1;
}
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
//--------------------------------
// Call inet_addr(). If the call succeeds,
// the result variable will hold a IN_ADDR
ulAddr = inet_addr(argv[1]);
if ( ulAddr == INADDR_NONE ) {
printf("inet_addr failed and returned INADDR_NONE\n");
WSACleanup();
return 1;
}   
if (ulAddr == INADDR_ANY) {
printf("inet_addr failed and returned INADDR_ANY\n");
WSACleanup();
return 1;  
}
printf("inet_addr returned success\n");
// Retrieve each address and print out the hex bytes
//    for(i=0, ptr= (Char*) &ulAddr; i < 4; i++, ptr++) {
WSACleanup();
return 0;
}
程序示例2：
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
int main(int argc, char *argv[])
{
struct in_addr addr1,addr2;
ulong l1,l2;
l1= inet_addr("192.168.0.74");
l2 = inet_addr("211.100.21.179");
memcpy(&addr1, &l1, 4);
memcpy(&addr2, &l2, 4);
printf("%s : %s\n", inet_ntoa(addr1), inet_ntoa(addr2)); //注意这一句的运行结果
printf("%s\n", inet_ntoa(addr1));
printf("%s\n", inet_ntoa(addr2));
return 0;
}
实际运行结果如下：
192.168.0.74 : 192.168.0.74 //从这里可以看出,printf里的inet_ntoa只运行了一次。
192.168.0.74
211.100.21.179
inet_ntoa返回一个char *,而这个char *的空间是在inet_ntoa里面静态分配的，所以inet_ntoa后面的调用会覆盖上一次的调用。第一句printf的结果只能说明在printf里面的可变参数的求值是从右到左的，仅此而已。　　　　
程序示例3：
将示例2修改一下，使其运行在VC++6.0下：
#include <stdio.h>
#include <string.h>
#include <WINSOCK2.h>
#pragma comment(lib,"WS2_32.LIB")
int main(int argc, char *argv[])
{
struct in_addr addr1,addr2;
u_long l1,l2;
l1 = inet_addr("192.168.0.74");
l2 = inet_addr("211.100.21.179");
memcpy(&addr1, &l1, 4);
memcpy(&addr2, &l2, 4);
printf("%s : %s\n", inet_ntoa(addr1), inet_ntoa(addr2)); 
printf("%s\n", inet_ntoa(addr1));
printf("%s\n", inet_ntoa(addr2));
return 0;
} 
程序示例4：
将示例1修改一下，使其运行在VC2008下：
#include <winsock2.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib,"WS2_32.lib")
#include <Windows.h>
// link with Ws2_32.lib 　　
int __cdecl main(int argc, char **argv)
{
//----------------------------------------- 　　
// Declare and initialize variables 　　
WSADATA wsaData;
int iResult;
unsigned long ulAddr = INADDR_NONE;
// Validate the parameters 　　
if (argc != 2) 
{
printf("usage: %s <IPv4 address>\n", argv[0]);
printf(" inetaddr converts a string containing an\n");
printf(" IPv4 address in one of the supported formats\n");
printf(" to a unsigned long representing an IN_ADDR\n");
printf(" %s 192.168.16.34\n", argv[0]);system("pause");
return 1;
}// Initialize Winsock 　　
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0)
{
printf("WSAStartup failed: %d\n", iResult);system("pause");
return 1;
}//-------------------------------- 　　
// Call inet_addr(). If the call succeeds, 　　
// the result variable will hold a IN_ADDR 　　
ulAddr = inet_addr(argv[1]);
if ( ulAddr == INADDR_NONE )
{
printf("inet_addr failed and returned INADDR_NONE\n");
WSACleanup();system("pause");
return 1;
}
if (ulAddr == INADDR_ANY)
{
printf("inet_addr failed and returned INADDR_ANY\n");
WSACleanup();system("pause");
return 1;
}
printf("inet_addr returned success\n");
// Retrieve each address and print out the hex bytes 　　
// for(i=0, ptr= (Char*) &ulAddr; i < 4; i++, ptr++) { 　　
WSACleanup();
system("pause");
return 0;
}