structure initialization

There are 3 ways to initialize the structure

The structure is defined as follows

1 struct obj_type
2 {
3     char a;
4     int b;
5     float c;
6     double d;
7 }

 

The first kind, the position corresponds to the assignment

Position correspondence must be listed in sequence

1 struct obj_type obj =
2 {
3     10,
4     1000,
5     1.1,
6     1.1111
7 }

Parameters not listed in order are irrelevant, but default parameters must be last

struct obj_type obj =
{
    10,
    1000
}

 

Type 2, point access assignment

The dot access can refer to where to play, assign values ​​to the parameters of interest, the irrelevant parameters are defaulted, and the assignment items are clear and clear

1 struct obj_type obj =
2 {
3     .a =10,
4     .c =1.1
5 }

 

Type 3, colon indicates assignment

same-point assignment

1 struct obj_type obj =
2 {
3     a: 10,
4     c: 1.1
5 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325982808&siteId=291194637