我遇到了以下代码快照:
struct hostent *hp;
hp = my_gethostbyname(localhost);
if (hp == NULL) {
ls_syslog(LOG_ERR, I18N_FUNC_FAIL, fname, "my_gethostbyname()");
return -1;
}
strcpy(localhost, hp->h_name);
memcpy(&addr, hp->h_addr, hp->h_length);
Run Code Online (Sandbox Code Playgroud)
我对最后一个语句感到困惑,struct hostent的声明是这样的:
struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* length of address */
char **h_addr_list; /* list of addresses */
};
Run Code Online (Sandbox Code Playgroud)
它没有名为"h_addr"的字段,但代码确实可以编译,任何人都可以告诉我为什么?谢谢.
Ign*_*ams 24
你错过了这一点:
#define h_addr h_addr_list[0] /* for backward compatibility */
Run Code Online (Sandbox Code Playgroud)
所以不,没有问题.