Problems in the nested structure

Structure of the self-reference (Reference Self) , that is, the internal structure comprising a pointer type structure itself.

Structure refer to each other (Mutual Reference) , that is to say in a plurality of the structures, it contains a pointer to another structure.

1. The self-referential structure

1.1  When not in use typedef

Wrong way:

struct tag_1{
    struct tag_1 A;  
    int value;
};

        This statement is wrong , because this statement is actually an infinite loop, A is a member of a structure, there will be internal A member of a structure, turn down, wireless loop. When memory allocation, since the infinite nesting, can not determine the length of the structure, so this approach is illegal.

Right way:  (using a pointer )

struct tag_1{
    struct tag_1 *A; 
    int value;
};

        Since the length of the pointer is determined (on 32-bit machines pointer length of 4), the compiler is able to determine the length of the structure.

1.2 Use typedef

Wrong way:

typedef struct {
    int value;
    NODE *link; 
} NODE;

  The purpose here is to use typedef to create an alias for the NODEP structure. But here it is wrong , because the scope of the type name is the statement from the end of the beginning, and in the internal structure can not be used, because no definition.

Right way: There are three very different, which can use.

typedef struct tag_1{
    int value;
    struct tag_1 *link; 
} NODE;


struct tag_2;
typedef struct tag_2 NODE;
struct tag_2{
    int value;
    NODE *link;   
};


struct tag_3{
    int value;
    struct tag_3 *link; 
};
typedef struct tag_3 NODE;

2. The structure of each reference

Wrong way:

typedef struct tag_a{
    int value;
    B *bp; 
} A;

typedef struct tag_b{
    int value;
    Come * up;
} B;

       And the cause of the error as above, type B was used here before defined.

The right way :( use the " incomplete statements ")

struct tag_a{
    struct tag_b *bp; 
    int value;
};
struct tag_b{
    struct tag_a *ap;
    int value;
};
typedef struct tag_a A;
typedef struct tag_b B;



struct tag_a;  
struct tag_b;
typedef struct tag_a A;
typedef struct tag_b B;
struct tag_a{
    struct tag_b *bp; 
    int value;
};
struct tag_b{
    struct tag_a *ap;
    int value;
};

Nested structure should pay attention to:

Self-referential structure, the following case is illegal

struct s_ref {
 int a;
 struct s_ref b;
 char c;
};

Because the internal structure also contains its own structure type b, this length can not be determined, only then look down, but also contains its own structure type b, look down again, and so on, similar to the outlet never recursive calls, It is illegal.

But very often, really need to use a self-referencing, it is a skill, as follows:

struct , s_ref, {
  int A;
  struct , s_ref, * B;   // Note that the same position as the above phrase distinction 
 char C;
};

This is legal because here is the definition of a pointer pointing to the structure, size of the pointer in the specific machine and compiler platform environments are known (even if different platforms defined environments are not identical). So it will not lead to the above recursive infinite loop. It is legitimate and viable. But to remind that: the pointer appears to point to itself, not really, but point to the different structures of the same type.
Lists and data on the use of trees to this technique. Its structure pointer to the next node or a next address sub-tree.

There is a case worthy of note:

typedef struct {    // this is a structure type definition 
 int A;
 , s_ref, * b;   // Note the phrase quoted a structure type name 
 char c;
}s_ref ;

This structure type definition is to define the type name s_ref, but failed. Because the reference to the structural body structure type name, type name is not defined yet at this time.

It can be read as follows:

typedef struct s_ref_t {    // this is a structure type definitions and structure label 
 int A;
  struct s_ref_t * B;   // Note that the same phrase distinguished from the upper position, using the tag 
 char C;
}s_ref ;

Here you will run well.

Nested problem structure - Renzhi Kang - blog Park: Portal 

https://www.cnblogs.com/renyuan/archive/2012/11/30/2796792.html

 

Guess you like

Origin www.cnblogs.com/tongongV/p/11008473.html