简体   繁体   English

为什么允许结构本身具有“指向其自身类型的指针”而不是成员本身(“结构类型的数组”)?

[英]Why a structure is allowed to have “pointer to its own type” as member but not “(an array of the) structure type” itself?

when i try to declare the following function 当我尝试声明以下功能时

typedef struct TRIE_NODE
{
    char* word;
    struct TRIE_NODE node[26];
}TRIE_NODE;

I get the following error: 我收到以下错误:

definition of 'struct TRIE_NODE' is not complete until the closing '}' 直到结束“}”时,“ struct TRIE_NODE”的定义才完整

However, if i declare this function with a pointer to the 26 nodes, it compiles just fine. 但是,如果我用指向26个节点的指针声明此函数,则编译就很好。

typedef struct TRIE_NODE
{
    char* word;
    struct TRIE_NODE* node[26];
}TRIE_NODE;

I imagine that, since this is not an instance, it's impossible for me to get a pointer to the first of those 26 arrays, but if that is the problem, how is TRIE_NODE* node[26] not also a problem? 我可以想象,由于这不是实例,因此我不可能获得指向这26个数组中第一个数组的指针,但是如果这是问题所在,那么TRIE_NODE* node[26]也不是问题吗? Isn't this declaration equivalent to TRIE_NODE node[1][26] ? 这个声明不等于TRIE_NODE node[1][26]吗?

when i try to declare the following function 当我尝试声明以下功能时

Wait!! 等待!! that's not a function, that's typedef -ing a structure, a user-defined type. 这不是一个函数,这是typedef -ing的结构,一个用户定义的类型。


That said, in the first case, 也就是说,在第一种情况下,

typedef struct TRIE_NODE
{
    char* word;
    struct TRIE_NODE node[26];  //array of type struct TRIE_NODE
}TRIE_NODE;

if this has to be possible, compiler needs to know the size of the struct TRIE_NODE before it has been defined, which is impossible. 如果必须这样做,则编译器需要在定义struct TRIE_NODE 之前知道其大小,这是不可能的。 So it is invalid. 因此无效。

On th other hand, 另一方面,

typedef struct TRIE_NODE
{
    char* word;
    struct TRIE_NODE* node[26];  //array of pointers (of type struct TRIE_NODE)
}TRIE_NODE; 

is fine, as you're allocating (array of) pointers to the structure, the actual size of the structure is not required to be known by compiler at that point. 很好,因为您正在分配指向结构的指针(的数组),所以此时编译器不需要知道结构的实际大小。 So, the compiler happily allocates the pointers and the definition (construct) is perfectly valid. 因此,编译器愉快地分配了指针,并且定义(构造)完全正确。

To answer your own question, ask: how many bytes will 要回答您自己的问题,请询问:多少字节

struct TRIE_NODE node[26];

occupy? 占据? In fact, what would you expect sizeof(struct TRIE_NODE) to be? 实际上,您希望sizeof(struct TRIE_NODE)是什么?

The reason 原因

struct TRIE_NODE *node[26];

works is that we know the value of sizeof(struct TRIE_NODE*) . 起作用的是,我们知道sizeof(struct TRIE_NODE*) Because all struct pointers have the same size , we can allocate an array of N struct pointers, no matter their type, even if incompletely defined. 因为所有结构指针的大小都相同 ,所以即使它们的类型不完整,我们也可以分配N个结构指针的数组。

aren't pointers and arrays almost interchangeable? 指针和数组几乎不可以互换吗?

The syntax for pointers and arrays is similar. 指针和数组的语法相似。 You can subscript a pointer, and you can add to an array's address. 您可以为指针下标,也可以添加到数组的地址。 But they define different things. 但是他们定义了不同的东西。 Basically: an array holds data, and a pointer holds an address. 基本上:数组保存数据,而指针保存地址。

In certain parts of the C standard library, you'll find structures defined like this: 在C标准库的某些部分,您会发现定义如下的结构:

struct S {
    int len;
    char data[1];
};

You might be tempted to ask why not use a pointer? 您可能会想问为什么不使用指针?

struct Z {
    int len;
    char *data;
};

Answer: struct S is actually bigger than the 5 or so bytes it seems to occupy, and the data portion begins immediately after len . 答案: struct S实际上大于它似乎占据的5个左右字节,并且数据部分在len之后立即开始。 In the putative struct Z example, data doesn't begin the data; 在假定的struct Z示例中, data不是数据开始的; the data would be somewhere else, wherever data points . 数据将在其他任何地方,无论data 指向何处。

Assuming the structures are appropriate initialized, in both cases data[0] will address the first byte of the array. 假设适当地初始化了结构,则在两种情况下data[0]都将寻址数组的第一个字节。 They're syntactically similar. 它们在语法上相似。 But the memory layout is different. 但是内存布局不同。 In the S case, that byte will be pretty close to (char*)&len + sizeof(len) . S情况下,该字节将非常接近(char*)&len + sizeof(len) In the Z case it will be wherever data points. Z情况下,它将位于data点的任何位置。

没有人提到这一点,但是如果允许struct拥有其自身的成员或数组(不是指针,而是常规成员或数组),则该结构将是递归的并且具有无限大小,因为该成员在同一个成员中还会有一个成员等等,直到无穷大。

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

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