为什么 h_addr_list (在主机结构中)被输入为 char** 而不是 void*? [英] Why is h_addr_list (in hostent struct) typed as char** and not void*?

查看:29
本文介绍了为什么 h_addr_list (在主机结构中)被输入为 char** 而不是 void*?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出一段构建宿主对象的代码,更具体地说,它的工作部分是填充 h_addr_list 数组.

I was trying to figure out a piece of code which constructs a hostent object, more specifically, the part of it's work that populates the h_addr_list array.

我对分配到数组中的值被强制转换为 char* 感到困惑,因为就我阅读而言,这个数组与字符串无关.

I was confused by the fact that the values that were assigned into the array were cast to char*, seeing as this array has nothing to do with strings as far as I read.

我注意到 h_addr_list 数组实际上被输入为 char**.

I noticed that the h_addr_list array is actually typed as char**.

我看到的唯一注意到这一事实的地方是第 9.7 节在 beej 的网络编程指南中,但即使如此也没有解释为什么这样输入.

The only place I saw that takes note of this fact is section 9.7 in beej's Guide to Network Programming, but even there there's no explanation on why is it typed so.

h_addr_list 被输入为 char** 而不是 void*/void** 是否有原因>(或其他可能提供更多信息的东西)?

Is there a reason that h_addr_list is typed as char** and not void*/void** (or anything else that might be a bit more informative)?

推荐答案

char * 不一定指的是一个 C 字符串,但也指的是一段已知长度的内存块按字节寻址,大概是因为它来自网络.

char * needn't necessarily refer to a C string, but also to a chunk of memory of known length that needs to be addressed by byte, presumably because it arrives from the network.

在像 int8_tuint8_t 这样的类型被添加到 C99 之前,char * 是引用连续字节数组的唯一实用方法.事实上,struct hostent 早于 C89,后者形式化了 void 类型.如果今天编写,该字段将被声明为 void ** 或可能是 uint8_t **.

Before types like int8_t and uint8_t were added to C99, char * was the only practical way to refer to a contiguous byte array. In fact, struct hostent predates C89 which formalized the void type. If written today, the field would be declared as void ** or possibly uint8_t **.

这篇关于为什么 h_addr_list (在主机结构中)被输入为 char** 而不是 void*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆