结构体学习笔记4——结构体成员赋值

#include <stdio.h>
#include <stdlib.h>

struct Stu  
{
    char name[10];
    int  age;
    double high;
};
int main(void)
{
    struct Stu stu1 = { "大饼",23,1.90 };
    //stu1.age = 29;  成员单个赋值
    //stu1.high = 2.20;
    //strcpy(stu1.name, "飞扬");//字符数组 必须用strcpy
    ////上面这种赋值方法有些麻烦  一个新的方法:c99标准 复合文字结构
    stu1 = (struct Stu) { "轻舞", 31, 1.76 };
    printf("%s,%d,%lf
", stu1.name, stu1.age, stu1.high);
    struct Stu *p = &stu1;
    printf("%s,%d,%lf
", p->name, p->age, p->high);


    system("pause");
    return 0;
}

如果想初始化指定的元素?比如只想初始化age?!

struct Stu stu1 = {.age=199 };

不过貌似用处不大!

原文地址:https://www.cnblogs.com/dabing0983/p/10531716.html