21

I encountered the following code snapshot:

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);

I am rather confused by the last statement, the declaration of struct hostent is like this:

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 */
};

It doesn't have a field named "h_addr", but the code did can compile, can anyone tell me why? thanks.

4

4 に答える 4

28

あなたはその真下でこのビットを逃しました:

#define h_addr h_addr_list[0] /* for backward compatibility */

いいえ、問題ありません。

于 2012-07-10T02:36:21.587 に答える
3

GNUのドキュメンテーションページで彼らは言う

「ホストは複数のネットワークに接続され、それぞれに異なるアドレスを持つ可能性があることを思い出してください」

h_addrまた、ベクトルの最初の要素であるa も提供しh_addr_listます。

于 2015-01-22T04:37:05.230 に答える