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

痞子三分冷 提交于 2019-12-02 02:49:21

when i try to declare the following function

Wait!! that's not a function, that's typedef-ing a structure, a user-defined type.


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. 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?

The reason

struct TRIE_NODE *node[26];

works is that we know the value of 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.

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:

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. In the putative struct Z example, data doesn't begin the data; the data would be somewhere else, wherever data points.

Assuming the structures are appropriate initialized, in both cases data[0] will address the first byte of the array. 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). In the Z case it will be wherever data points.

Nobody mention this, but if struct was allowed to have member or array of itself (not a pointer, but regular member or array), then the struct would be recursive and with infinite size, because the member will have one more member inside of same type and so on until infinity.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!