结构体的三种初始化方法


转载:https://blog.csdn.net/a_ran/article/details/44755759 

结构体的三种初始化方式

#include <stdio.h>

struct student_st

{

       char c;

       int score;

       const char *name;

};

 

static void show_student(struct student_st *stu)

{

       printf("c = %c, score = %d, name = %s\n", stu->c, stu->score, stu->name);

}

 

int main(void)

{

       // method 1: 按照成员声明的顺序初始化

       struct student_st s1 = {'A', 91, "Alan"};

       show_student(&s1);

 

       // method 2: 指定初始化,成员顺序可以不定,Linux 内核多采用此方式

       struct student_st s2 =

       {

              .name = "YunYun",

              .c = 'B',

              .score = 92,

       };

       show_student(&s2);

 

       // method 3: 指定初始化,成员顺序可以不定

       struct student_st s3 =

       {

              c: 'C',

              score: 93,

              name: "Wood",

       };

       show_student(&s3);

        return 0;

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM