当前位置:网站首页>C language structure specifying initialization

C language structure specifying initialization

2022-04-23 06:48:00 tilblackout

Many times we don't use every member of the structure , Just want to initialize some of the members , At this time, you can use the specified initialization of the structure .

struct student {
    char *name;
    int num;
    int age;
    char group;
    float score;
} ;

(1) Under normal circumstances : All variables must be initialized 
struct student stu1 ={"Tom",12,18,"A",136.5};

(2) Specified initialization : Add a... Before the variable name “.”, Each line is separated by commas 
struct student stu1={
    .score=136.5,
    .age=18,
    .group="A",
} ;

example :

struct test{
    int a;
    char b;
};
struct test c[] = {// Create three members , Partial initialization 
    [0] = {
        .a = 1,
    },
    [2] = {
        .b = 'a',
    },
};

版权声明
本文为[tilblackout]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230549282435.html