MSDN:The inet_ntoa function converts an (Ipv4) Internet network address into an ASCII string in Internet standard dotted-decimal format.
Syntax
char* FAR inet_ntoa( _In_ struct in_addr in );
Parameters
-
in [in]
-
An in_addr structure that represents an Internet host address.
Return value
If no error occurs, inet_ntoa returns a character pointer to a static buffercontaining the text address in standard ".'' notation. Otherwise, it returns NULL.
注意:返回的是静态指针。每次调用Inet_ntoa(),它就会覆盖上次调用时所使用的IP地址(内存被覆盖) 。例如:
char *a1, *a2;
a1 = inet_ntoa(ina1.sin_addr); /* 这是198.92.129.1 */
a2 = inet_ntoa(ina2.sin_addr); /* 这是132.241.5.10 */
printf("address 1: %s\n",a1);
printf("address 2: %s\n",a2);
输出如下:
address 1: 132.241.5.10
address 2: 132.241.5.10
若要长久保存IP地址,还要事先定义一个内存区域,并将当前的静态区域内的IP地址拷贝到新定义的内存区域。