简体   繁体   English

如何在C中访问此结构的成员?

[英]How do I access members of this structure in C?

I'm working with this structure in C: 我正在C中使用此结构:

/** This structure describes an Internet host address. */
typedef struct pj_hostent
{
    char    *h_name;        /**< The official name of the host. */
    char   **h_aliases;     /**< Aliases list. */
    int      h_addrtype;    /**< Host address type. */
    int      h_length;      /**< Length of address. */
    char   **h_addr_list;   /**< List of addresses. */
} pj_hostent;

I can access the h_name part of the structure fine like this: 我可以像这样很好地访问结构的h_name部分:

strcpy(test1, he->h_name); // copy part of struct into char[] array

and it contains a meaningful "sip2" value. 并且包含有意义的“ sip2”值。 However, when I try to access the elements of h_addr_list like this: 但是,当我尝试像这样访问h_addr_list的元素时:

strcpy(test1, he->h_addr_list[0]);

I get meaningless jibberish. 我变得毫无意义。

What's the correct way in C to access values like this? 在C中访问这样的值的正确方法是什么?

h_addr_list[0] is not a string, it is a pj_in_addr , which is a 32-bit integer which is not null terminated. h_addr_list[0]不是字符串,而是pj_in_addr ,它是一个不为null终止的32位整数。

printf() it with %d or %x , not %s . printf()使用%d%x而不是%s

See PJLIB Reference: Network Address Resolution for an example of use. 有关使用示例,请参见PJLIB参考:网络地址解析

You are accessing it correctly. 您正在正确访问它。 That field really does contain gibberish. 该字段确实包含乱码。

Just because it is declared as a char** does NOT mean that it is an array of strings. 只是因为它被声明为一个char** 并不意味着它是一个字符串数组。

It may be a pointer to a byte-buffer of unknown format. 它可能是指向未知格式的字节缓冲区的指针。 (ie. jibberish) (即胡言乱语)

You should check any available documentation or other source code for details of exactly what h_addr_list is, and how it should be interpreted. 您应该检查任何可用的文档或其他源代码,以获取有关h_addr_list的确切含义以及应如何解释的详细信息。

The struct only contains a pointer. 该结构仅包含一个指针。 You need to allocate some storage for that pointer to point at before you can use it. 您需要先分配一些存储空间以供该指针指向然后才能使用它。 As it is, the pointer almost certainly just contains a (more or less) random value. 实际上,指针几乎可以肯定包含(或多或少)随机值。 When you use that as a target in your call to strcpy , you're overwriting that random memory location -- which can and will lead to major problems elsewhere. 当您将其用作对strcpy的调用的目标时,您将覆盖该随机内存位置-这可能并且将在其他地方导致重大问题。

h_addr_list is pretty much the same way -- it's just a pointer to a pointer. h_addr_list几乎是相同的方式-只是一个指向指针的指针。 That'll (normally) be used to refer to an array of strings, but something has to allocate that array of strings before it can be put to any meaningful use. (通常)将用于指代字符串数组,但是必须先分配该字符串数组,然后才能将其用于任何有意义的用途。 Given the similarity to a hostent structure, along with this structure there's presumably some analog to (or wrapper around) gethostbyname and/or gethostbyaddress that initializes this. 考虑到与hostent结构的相似性,以及与该结构的相似性,可能存在一些类似于(或包装) gethostbyname和/或gethostbyaddress初始化类。 In the case of the wrapper (which I'd judge the more likely of the two), it probably gets the correct data in a hostent , and then copies data from the hostent to this structure. 在包装器的情况下(我认为这是两者中比较有可能的),它可能会在hostent获取正确的数据,然后将数据从hostent复制到此结构中。

A short Example that does work. 一个有效的简短示例。 Remember that allocating for the struct does not allocate for the pointer. 请记住,为结构分配不会为指针分配。

typedef struct pj_hostent
{
    char    *h_name;        /**< The official name of the host. */
    char   **h_aliases;     /**< Aliases list. */
    int      h_addrtype;    /**< Host address type. */
    int      h_length;      /**< Length of address. */
    char   **h_addr_list;   /**< List of addresses. */
} pj_hostent;

int _tmain(int argc, _TCHAR* argv[])
{
    char* s[]= {"this","is","couple of words"};
    pj_hostent hst = {"high",s,3,4,s };
    char  buff[255] = {};
    //Access as a local initialized struct
    strcpy(buff,hst.h_addr_list[0]);
    printf("%s\n",buff);
    pj_hostent * he = & hst; 
    //Access as a pointer 
    strcpy(buff,he->h_addr_list[0]);
    printf("%s\n",buff);
    gets(buff);
}

如果您从未为这些指针初始化/分配内存,则将出现乱码和/或分段错误,因为它们指向内存中的随机点。

strcpy assumes that you have already allocated memory to the character pointer. strcpy假定您已经为字符指针分配了内存。 So, probably, the pointer is still having a NULL value. 因此,指针可能仍具有NULL值。

struct elements are accessed with the dot operator 使用点运算符访问struct元素

strcpy(test1, he.h_addr_list[0]);

elements of structs that are "pointed" to are accessed with the -> operator 使用->运算符访问“指向”结构的元素

strcpy(test1, he->h_addr_list->[0]);

If you're getting gibberish, then either test1 isn't big enough, h_addr_list isn't pointing to an array of char*, or the first element of the array is actually gibberish. 如果您变得乱码,则要么test1不够大,h_addr_list指向的不是char *数组,或者该数组的第一个元素实际上是乱码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM