关于inet_ntoa返回的值是静态变量

来源:互联网 发布:js删除浏览器cookie 编辑:程序博客网 时间:2024/05/16 06:22

今天终于以前写程序就在inet_ntoa()出的问题:
The inet_ntoa() function converts the Internet host address in given   in  network     byte     order   to   a   string   in   standard   numbers-and-dots   notation.  The   string   is   returned   in   a   statically   allocated   buffer,     which     subsequent   calls   will   overwrite.  
 说明它是一个由inet_ntoa() 控制的静态的固定的指针,所以每次调用inet_ntoa() ,它就将覆盖上次调用时所得的IP地址。例如:
  char* strA = inet_ntoa(s_a);
  char* strB = inet_ntoa(s_b);

  lstrcpyA(threadparam[i].IPAstr, strA);
  lstrcpyA(threadparam[i].IPBstr, strB);

假如你需要保存这个IP地址,使用lstrcpyA()函数来指向你自己的字符 指针。  

应该如下:

  char* strA = inet_ntoa(s_a);

  lstrcpyA(threadparam[i].IPAstr, strA);
  char* strB = inet_ntoa(s_b);

  lstrcpyA(threadparam[i].IPBstr, strB);
 所以,你如果需要多次结果,应该没调用一次,使用strcpy复制到你的缓冲区(这种解决方法有待于亲自考证) 。

原创粉丝点击