连续多次调用inet_ntoa()结果重复


#include <stdio.h> #include <stdlib.h> #include <string.h> #include <pcap.h> #include <arpa/inet.h> #include "myerror.h" int main(int argc, char* argv[]) { struct in_addr net_ip_address; struct in_addr net_mask_address; u_int32_t net_ip; u_int32_t net_mask; char errbuff[PCAP_ERRBUF_SIZE]; char* net_ip_string; char* net_mask_string; char* net_interface_string; if((net_interface_string=pcap_lookupdev(errbuff))==NULL) err_sys("lookup dev error"); if(pcap_lookupnet(net_interface_string,&net_ip,&net_mask,errbuff)!=0) err_sys("lookup net error"); net_ip_address.s_addr=net_ip; net_mask_address.s_addr=net_mask; if((net_ip_string=inet_ntoa(net_ip_address))==NULL) err_sys("ip ntoa error"); if((net_mask_string=inet_ntoa(net_mask_address))==NULL) err_sys("mask ntoa error"); printf("Network interface is %s\n",net_interface_string); printf("Network ip is %s\n",net_ip_string); printf("Network mask is %s\n",net_mask_string); return 0; }

 在一个生存周期内,inet_ntoa返回一个char *,而这个char *的空间是在inet_ntoa里面静态分配的,所以inet_ntoa后面的调用会覆盖上一次的调用。

第一句printf的结果只能说明在printf里面的可变参数的求值是从右到左的,仅此而已。

转载于:https://www.cnblogs.com/studystudent/p/3322442.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值