Why don't we get an error when we define Structure in C for a linked list

Take an example of the structure defined for a linked list ...

struct test_struct                                         line 1    
{                                                          line 2    
    int val;                                               line 3
    struct test_struct *next;                              line 4
};                                                         line 5

In line 4, since test_struct is not even completely defined (I assume that the structure is completely defined in line 5 due to ';', before that we can’t say that the structure is defined), then why don’t we get an error with line 4, that test_struct is not defined ...?

+4
source share
2 answers

It is true that it is struct test_structnot completely defined before closing ;, and in line 4 you define a pointer to an incomplete type, which is fine.

struct test_struct, . - .

, :

struct test_struct                                         
{                                                          
    int val;                                               
    struct test_struct value;                              
};

value . struct test_struct* .

+5

. () ( 4)! , free malloc. , . C , , .

+1

Source: https://habr.com/ru/post/1533971/


All Articles