inet_ntoa时,为什末去掉s_addr

ccjsj1 2011-09-21 01:37:03
inet_ntoa原型:char *inet_ntoa(struct in_addr in);

定义:struct sockaddr_in his_end;

inet_addr赋值时: his_end.sin_addr.s_addr=inet_addr("0.0.0.0");

inet_ntoa时:为什末去掉s_addr?

例如:printf("his_end=%s\n",inet_ntoa(his_end.sin_addr));



struct sockaddr_in {
sa_family_t sin_family; /* address family: AF_INET */
u_int16_t sin_port; /* port in network byte order */
struct in_addr sin_addr; /* internet address */
};

/* Internet address. */
struct in_addr {
u_int32_t s_addr; /* address in network byte order */
};
...全文
82 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccjsj1 2011-09-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 luciferisnotsatan 的回复:]
char *inet_ntoa(struct in_addr in);
参数要求的是struct in_addr,当然要传个struct in_addr给它。
sin_addr是struct in_addr类型,s_addr是里面的一个成员变量u_int32_t类型。
inet_addr的返回值是个32位整数
[/Quote]

谢谢,后来想明白了。
luciferisnotsatan 2011-09-21
  • 打赏
  • 举报
回复
char *inet_ntoa(struct in_addr in);
参数要求的是struct in_addr,当然要传个struct in_addr给它。
sin_addr是struct in_addr类型,s_addr是里面的一个成员变量u_int32_t类型。
inet_addr的返回值是个32位整数
ccjsj1 2011-09-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 luciferisnotsatan 的回复:]
去掉?这是什么意思??不就是把s_addr转成字符串么?
[/Quote]

问的是这个意思,赋值时带s_addr,inet_ntoa函数不带s_addr
inet_addr赋值时: his_end.sin_addr.s_addr=inet_addr("0.0.0.0");

inet_ntoa时:为什末去掉s_addr?

例如:printf("his_end=%s\n",inet_ntoa(his_end.sin_addr));
luciferisnotsatan 2011-09-21
  • 打赏
  • 举报
回复
去掉?这是什么意思??不就是把s_addr转成字符串么?
tony2278 2011-09-21
  • 打赏
  • 举报
回复
帮顶·
ccjsj1 2011-09-21
  • 打赏
  • 举报
回复
inet_ntoa原型:char *inet_ntoa(struct in_addr in);

是不是因为inet_ntoa函数的参数要求是struct in_addr类型的结构名称,而不是结构中的成员。

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧