When is a structure (user data type) created?

for example, consider:

struct strct
{
 data member_1;
 data member_2;
 ......
};

when the compiler recognizes

struct strct 

how is the data type? This is after line execution

struct strct

? Or after meeting the closing bracket of the structure definition?

+4
source share
2 answers

Ads are not running.

After reading

struct strct {

the compiler recognizes it struct strctas an incomplete type , which is a type that does not know the size. Since you can use pointers for incomplete types, this allows you to write something like this:

struct strct {
    struct strct *next; // <- allowed, a pointer doesn't need the size of the object pointed to
    int foo;
};

"" , struct strct , ( ).


: :

struct strct;

, , struct strct. . , , - (, ). C. , , - :

struct strct;

struct strct *strct_create(void);
strct_foo(struct strct *self);
strct_bar(struct strct *self, int x);
[...]

struct strct ,

+8

struct strct , . :

struct mystruct {
    int val;
    struct mystruct *next;
};

, next, struct mystruct . , , .

, , .

, , :

struct mystruct {
    int val;
    struct mystruct next;
};

, .

forward , :

struct mystruct;

, :

struct mystruct1;

struct mystruct2 {
    int val;
    struct mystruct1 *other;
};

struct mystruct1 {
    int val;
    struct mystruct2 *other;
};
+5

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


All Articles