[Definition, initialization and use of structure variables]

1. Definition and initialization of structure variables

A structure variable is a variable, which is a collection of several data.
Notice:

  1. Before defining a structure variable, you must first have a structure type, and then define the variable;
  2. When defining a structure variable, you can assign an initial value to the structure variable by the way, which is called the initialization of the structure;
  3. When a structure variable is initialized, each member is initialized sequentially.
    e.g.
#include <stdio.h>


//定义结构体类型
struct stu{
    
    
    int id;
    char name[32];
    char sex;
    int age;
    //定义结构体变量之:定义结构体类型的同时定义结构体变量
}alice, lisa = {
    
    1101, "Lisa_Wolf", 'F', 30};

//使用typedef对结构体类型取别名
typedef struct{
    
    
    int a;
    int b;
    char c;
}MSG;


int main(int argc, char *argv[])
{
    
    
    //定义结构体变量之:类型定义完毕之后定义变量
    struct stu mary;
    //结构体变量的初始化
    struct stu Anna = {
    
    1001, "安娜",'F',20};
    
    //如果使用typedef对结构体类型取别名
    //就无法在定义类型的同时定义结构体变量
    //在定义结构体变量的时候不用加struct
    MSG msg1, msg2 = {
    
    100, 200, 'w'};
    
    return 0;
}

Second, the use of structure variables

The way structure variables call members:

 结构体变量.结构体成员

Note: The structure variables mentioned here mainly refer to ordinary structure variables

Simple use of structure variables

#include <stdio.h>
#include <string.h>


//定义结构体类型
struct stu{
    
    
    int id;
    char name[32];
    char sex;
    int age;
    //定义结构体变量之:定义结构体类型的同时定义结构体变量
}Alice, lisa = {
    
    1101, "Lisa_Wolf", 'F', 30};

//使用typedef对结构体类型取别名
typedef struct{
    
    
    int a;
    int b;
    char c;
}MSG;


int main(int argc, char *argv[])
{
    
    
    //定义结构体变量之:类型定义完毕之后定义变量
    struct stu Mary;
    //结构体变量的初始化
    struct stu Anna = {
    
    1001, "Anna Benz",'F',20};

    //如果使用typedef对结构体类型取别名
    //就无法在定义类型的同时定义结构体变量
    //在定义结构体变量的时候不用加struct
    MSG msg1, msg2 = {
    
    100, 200, 'w'};

    //结构体变量的使用
    Alice.id = 1001;
    strcpy(Alice.name, "Alice Granthm");
    Alice.sex = 'F';
    Alice.age = 20;

    printf("%d - %s - %c - %d\n", Alice.id, Alice.name, Alice.sex, Alice.age);

    printf("%d - %s - %c - %d\n", lisa.id, lisa.name, lisa.sex, lisa.age);

    printf("%d - %s - %c - %d\n", Mary.id, Mary.name, Mary.sex, Mary.age);

    printf("%d - %s - %c - %d\n", Anna.id, Anna.name, Anna.sex, Anna.age);

    printf("%d - %d - %c\n", msg2.a, msg2.b, msg2.c);

    return 0;
}

Results of the
struct

Nesting structs within structs

#include <stdio.h>
#include <string.h>


//在结构体中嵌套结构体

typedef struct{
    
    
    int year;
    int month;
    int day;
}BD;

typedef struct{
    
    
    int id;
    char name[32];
    BD birthday;
}Student;

void test2()
{
    
    
    Student Anna;
    Anna.id = 1101;
    strcpy(Anna.name, "Anna Benz");
    //如果结构体中嵌套结构体,赋值时找到最内层的成员再进行赋值
    Anna.birthday.year = 2002;
    Anna.birthday.month = 02;
    Anna.birthday.day = 20;

    printf("%d - %s - %d-%d-%d\n", Anna.id, Anna.name, Anna.birthday.year,Anna.birthday.month, Anna.birthday.day);

    //嵌套的形式定义并初始化
    Student Alice =  {
    
    1002, "Alice Klarkson", 2000, 3, 16};
    printf("%d - %s - %d-%d-%d\n", Alice.id, Alice.name, Alice.birthday.year,Alice.birthday.month, Alice.birthday.day);

}

int main(int argc, char *argv[])
{
    
    
    test2();

    return 0;
}

Results of the
insert image description here

Structure variables of the same type can be assigned to each other

#include <stdio.h>
#include <string.h>


//在结构体中嵌套结构体

struct student{
    
    
    int id;
    char name[32];
    char sex;
    int age;
};

int main(int argc, char *argv[])
{
    
    
    struct student Mary;
    Mary.id = 1201;
    strcpy(Mary.name, "Mary Branson");
    Mary.sex = 'F';
    Mary.age = 18;
    printf("%d - %s - %c - %d\n",Mary.id, Mary.name, Mary.sex, Mary.age);

    //相同类型的结构体变量之间可以直接赋值
    struct student Lisa;
    Lisa = Mary;
    printf("%d - %s - %c - %d\n", Lisa.id, Lisa.name, Lisa.sex, Lisa.age + 1);

    return 0;
}

Results of the
insert image description here

Guess you like

Origin blog.csdn.net/shuting7/article/details/130295827