C语言中结构体的初始化

直接上例子:

struct point

{   

  int x;   

  int y;   

  int z;

}

//常规写法

struct point pt1 = {100, 300, 200};

//初始化个数少于实际个数时,只初始化前面的成员

struct point pt2 = {400};

struct point pt3 = (struct point){100, 200, 300};

 

//网上说这叫乱序初始化,C99加的。

struct point pt4 = {

  .y = 100,

  .x = 400,

  .z = 0

};

//因为是初始化,如果向下面这样赋值就错了:

//struct point pt4;

//pt4 = {100,200,300};

//pt4 = {.z = 100};

 

你可能感兴趣的:(C语言中结构体的初始化)