C语言:结构体

一,结构体

  • C语⾔已经提供了内置类型,如:char、short、int、long、float、double等,但是只有这些内置类型还是不够的,假设我想描述学⽣,描述⼀本书,这时单⼀的内置类型是不⾏的。

  • 描述⼀个学⽣需要名字、年龄、学号、⾝⾼、体重等;

    • 描述⼀本书需要作者、出版社、定价等。C语⾔为了解决这个问题,增加了结构体这种⾃定义的数据类型,让程序员可以⾃⼰创造适合的类型。
      注 :结构是⼀些值的集合,这些值称为成员变量。结构的每个成员可以是不同类型的变量,如: 标量、数组、指针,甚⾄是其他结构体。

    1. 结构体的定义

    结构体的定义使用 struct 关键字,其基本语法如下:

    struct 结构体名 {
        数据类型 成员1;
        数据类型 成员2;
        // 可以有更多成员
    }variable-list;
    

    例如,定义一个表示学生信息的结构体:

    struct Student {
        char name[50];
        int age;
        float score;
    }variable-list;
    

    述代码定义了一个名为 Student 的结构体,它包含三个成员:一个字符数组 name 用于存储学生姓名,一个整型变量 age 用于存储学生年龄,一个浮点型变量 score 用于存储学生成绩。

    2. 结构体变量的声明

    定义好结构体后,可以声明该结构体类型的变量。有以下几种声明方式:

    方式一:先定义结构体,再声明变量
    struct Student 
    {
        char name[50];
        int age;
        float score;
    };
    
    int main() 
    {
        struct Student stu1; // 声明一个 Student 类型的变量 stu1
        return 0;
    }
    
    方式二:在定义结构体的同时声明变量
    struct Student
     {
        char name[50];
        int age;
        float score;
    } stu2; // 声明一个 Student 类型的变量 stu2
    
    int main()
     {
        // 使用 stu2
        return 0;
    }
    
    方式三:使用匿名结构体声明变量(匿名结构体声明变量只能声明一个)
    struct 
    {
        char name[50];
        int age;
        float score;
    } stu3; // 声明一个匿名结构体类型的变量 stu3
    
    int main() 
    {
        // 使用 stu3
        return 0;
    }
    

    3. 结构体变量的初始化

    结构体变量可以在声明时进行初始化,有以下几种初始化方式:

    方式一:按成员顺序初始化
    #include <stdio.h>
    
    struct Student 
    {
        char name[50];
        int age;
        float score;
    };
    
    int main() 
    {
        struct Student stu = {"Alice", 20, 85.5};
        printf("姓名: %s, 年龄: %d, 成绩: %.2f\n", stu.name, stu.age, stu.score);
        return 0;
    }
    
    方式二:指定成员初始化
    #include <stdio.h>
    
    struct Student 
    {
        char name[50];
        int age;
        float score;
    };
    
    int main() 
    {
        struct Student stu = {.name = "Bob", .score = 90.0, .age = 21};
        printf("姓名: %s, 年龄: %d, 成绩: %.2f\n", stu.name, stu.age, stu.score);
        return 0;
    }
    

    4. 结构体成员的访问

    可以使用点运算符(.)来访问结构体变量的成员。例如:

    #include <stdio.h>
    #include <string.h>
    
    struct Student
     {
        char name[50];
        int age;
        float score;
    };
    
    int main()
     {
        struct Student stu;
        strcpy(stu.name, "Charlie");
        stu.age = 19;
        stu.score = 78.5;
                                                //    结构体变量.结构体成员名
        printf("姓名: %s, 年龄: %d, 成绩: %.2f\n", stu.name, stu.age, stu.score);
        return 0;
    }
    

    5.结构体数组

    结构体数组是由多个相同结构体类型的元素组成的数组。例如:

    #include <stdio.h>
    
    struct Student 
    {
        char name[50];
        int age;
        float score;
    };
    
    int main() {
        struct Student students[3] = {
            {"David", 22, 88.0},
            {"Eve", 20, 92.5},
            {"Frank", 21, 76.0}
        };
    
        for (int i = 0; i < 3; i++) 
    {
            printf("姓名: %s, 年龄: %d, 成绩: %.2f\n", students[i].name, students[i].age, students[i].score);
        }
        return 0;
    }
    

    6. 结构体指针

    可以定义指向结构体的指针,通过指针访问结构体成员时需要使用箭头运算符(->)。例如:

    #include <stdio.h>
    #include <string.h>
    
    struct Student 
    {
        char name[50];
        int age;
        float score;
    };
    
    int main() 
    {
        struct Student stu = {"Grace", 23, 89.5};
        struct Student *p = &stu;
    
        printf("姓名: %s, 年龄: %d, 成绩: %.2f\n", p->name, p->age, p->score);
        return 0;
    }
    

    7. 结构体作为函数参数

    结构体可以作为函数的参数进行传递,有值传递和地址传递两种方式。

    值传递
    #include <stdio.h>
    
    struct Point 
    {
        int x;
        int y;
    };
    
    // 函数接受结构体值作为参数
    void printPoint(struct Point p) 
    {
        printf("坐标: (%d, %d)\n", p.x, p.y);
    }
    
    int main() 
    {
        struct Point pt = {3, 5};
        printPoint(pt);
        return 0;
    }
    
    地址传递
    #include <stdio.h>
    
    struct Point 
    {
        int x;
        int y;
    };
    
    // 函数接受结构体指针作为参数
    void modifyPoint(struct Point *p) 
    {
        p->x = 10;
        p->y = 20;
    }
    
    int main() 
    {
        struct Point pt = {3, 5};
        modifyPoint(&pt);
        printf("修改后的坐标: (%d, %d)\n", pt.x, pt.y);
        return 0;
    }
    
相关推荐
CHANG_THE_WORLD22 分钟前
C++并发编程指南04
开发语言·c++
轩情吖33 分钟前
二叉树-堆(补充)
c语言·数据结构·c++·后端·二叉树··排序
powershell 与 api40 分钟前
C#,shell32 + 调用控制面板项(.Cpl)实现“新建快捷方式对话框”(全网首发)
开发语言·windows·c#·.net
SomeB1oody44 分钟前
【Rust自学】19.2. 高级trait:关联类型、默认泛型参数和运算符重载、完全限定语法、supertrait和newtype
开发语言·后端·rust
山茶花开时。2 小时前
[SAP ABAP] 静态断点的使用
开发语言·sap·abap
纠结哥_Shrek2 小时前
Java 有很多常用的库
java·开发语言
加油,旭杏3 小时前
【go语言】函数
开发语言·后端·golang
摘星崖3 小时前
1.3编译流程与调试基础
c语言
代码对我眨眼睛3 小时前
重回C语言之老兵重装上阵(十三)C 预处理器
linux·c语言