Homing Structure Declaration

The current declaration is valid.

struct node
{
    int a;
    struct node *next;
};

      

However, when we define the following, it gives an error.

"error: field ‘next’ has incomplete type"

      

Why is this so?

struct node
{
    int a;
    struct node next; /* Not a pointer */   
};

      

+3


source to share


7 replies


node in struct node

is a "structure tag" which, at the point you write, creates an "incomplete type": a structure variable that is not currently declared but not defined. The type is not finalized until };

your final structure.

In C, an incomplete type can be referenced before it is fully defined by pointing to that type as a pointer. However, you cannot assign a variable (instance) of this type, because the actual structure definition has not yet been defined. (It works exactly the same as abstract base classes in C ++, if you're familiar with them.)

So when you write



struct node {
  int a;
  struct node *next;
};

      

the line struct node *next

means "here is a pointer to a node structure, although I have no idea how this type is defined yet." But you cannot declare a type variable struct node

inside a struct definition of the same type simply because you cannot use something before you create it.

+2


source


You cannot have a structure that contains itself as a member:

struct node
{
    int a;
    struct node next;
};

      



Think about this question: if possible, what is the size of such a structure? struct node

contains struct node next

as a member, then the member next

will contain a member of the type struct node

, and so on and on and on ... The size will be infinite.

+2


source


Your second declaration will define a structure that is infinitely deeply nested, which is impossible.

+1


source


This is the same case as when you explicitly declare class / struct type:

struct node;

struct node* node_ptr; /* is valid */
struct node node_instance; /* is invalid */

      

So struct node;

basically says: hey there is a structure defined somewhere outside of this file. The type is valid, pointers can be used, but you cannot instantiate an object.

This is because the size of the pointer is known and is specific to the target architecture (for example, 32 or 64 bit). The size of the structure is unknown prior to the announcement.

When you declare a completey type, you will be allowed to declare an object of that type:

struct node {
  int a;
  struct node* b; /* this will work, but the size of struct is unknown yet */
}

struct node* node_ptr; /* this works always with full and forward declarations */
struct node node_object; /* now, the size of struct is known, so the instance may be created */

      

+1


source


it should look like this:

struct node {
  int a;
  struct node *next;
};

      

it works,

but

struct node {
  int a;
  struct node next;
};

      

cannot be understood by the compiler because it node

becomes a recursive structure and the compiler does not know how much memory is allocated for node

.

However, if you use a pointer, it understands that the size of the pointer is equal to the size of the memory to be addressed, and therefore preserves that space, regardless of whether it is a node

complete structure.

0


source


The address of the store of pointers, the structure has a structure. If the structure declared would be recursive and infinite. If declared as a pointer, it refers to another structure somewhere else.

0


source


Infinite nodes inside a node? Does this make sense? What is the size of the "struct node"?

0


source







All Articles