【从零开始的 C 语言笔记】第二十八篇-Struct的介绍与应用

上一篇结束後我们就介绍了完整的变数生命周期了,也提到了区域变数、全域变数、自动变数、静态变数的概念,对於写程序来说搞懂变数的作用域是很重要的事情喔!

今天我们来介绍变数的组合技--「Struct」!


什麽是Struct?

到现在我们已经把单一变数的各种型态、作用域都介绍完成了,今天我们要来提到的是Struct(结构),Struct是一种自订的资料型态,可以把不同的基本资料型态(int.float.double.char)变数组合,形成新的资料型态。

也许这样说起来有一点笼统,不如我们来看下面的程序码:

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

struct students{
    int id;
    char name[20];
    int chinese;
    int english;
    int math;
    int social;
    int science;
};

int main()
{
    struct students one;
    
    one.id = 1;
    strcpy(one.name, "张小美");
    one.chinese = 77;
    one.english = 83;
    one.math = 86;
    one.social = 68;
    one.science = 91;

    printf("学生编号: %d\n", one.id);
    printf("姓名: %s\n", one.name);
    printf("国文成绩: %d\n", one.chinese);
    printf("英文成绩: %d\n", one.english);
    printf("数学成绩: %d\n", one.math);
    printf("社会成绩: %d\n", one.social);
    printf("自然成绩: %d\n", one.science);

    return 0;
}

https://ithelp.ithome.com.tw/upload/images/20211202/20142565EOiy65ub9z.png
可以看到在主函式外宣告了一个名为students的struct,而这个students中包含了int.char这些资料型态的变数,又与之前我们提过的阵列,都是同资料型态的样子大不相同。

struct比较像是为了描述某个资料而自订出来的,而为此我们会组合许多不同变数,如同资料表中会有许多不同栏位,但都是为了描述一笔一笔的资料而生。


正式使用

  1. struct定义的种类

(1) 纯粹定义struct

struct students{
    int id;
    char name[20];
    int chinese;
    int english;
    int math;
    int social;
    int science;
};

https://ithelp.ithome.com.tw/upload/images/20211202/20142565AjKBh5TBDp.png
日後需使用struct时的变数宣告:

struct students someone;

(2) 定义struct的同时宣告变数

struct students{
    int id;
    char name[20];
    int chinese;
    int english;
    int math;
    int social;
    int science;
}someone;

https://ithelp.ithome.com.tw/upload/images/20211202/20142565jCaqOFzUlR.png
日後使用时直接使用这个,已经被宣告好的变数someone就可以了。

(3) 定义struct也宣告struct别名

struct students{
    int id;
    char name[20];
    int chinese;
    int english;
    int math;
    int social;
    int science;
}
typedef struct students Student;

https://ithelp.ithome.com.tw/upload/images/20211202/20142565Vyz3CYqBmc.png
这样我们就替这个新struct取好Student的别名了,日後使用时的变数宣告会变为:

Student someone;

(4) 在定义struct时直接取别名

typedef struct{
    int id;
    char name[20];
    int chinese;
    int english;
    int math;
    int social;
    int science;
}Student;

https://ithelp.ithome.com.tw/upload/images/20211202/20142565dbKw99tQk3.png
日後的变数宣告:

Student someone;

(延伸阅读:李山姆的部落格-typedef struct用法说明)

  1. struct的使用
    当我们要使用struct时,除了一样要进行变数宣告外,我们要使用struct的成员时的用法,也与阵列有极大的不同。

(1) 变数宣告
与一般的变数宣告一样,都是采取「资料型态 变数名称;」的形式
(a) 无别名也未在结构宣告时同时宣告变数

struct students{
    int id;
    char name[20];
    int chinese;
    int english;
    int math;
    int social;
    int science;
};

struct students someone;

https://ithelp.ithome.com.tw/upload/images/20211202/20142565ycYNQEdJWj.png

(b) 已定义别名

typedef struct {
    int id;
    char name[20];
    int chinese;
    int english;
    int math;
    int social;
    int science;
}Student;

Student someone;

https://ithelp.ithome.com.tw/upload/images/20211202/20142565xh0ihY6PtC.png

(2) 取得、变更变数成员

one.id = 1;
printf("学生编号: %d\n", one.id);

https://ithelp.ithome.com.tw/upload/images/20211202/2014256568TSnTauXO.png

  1. 实际应用
#include <stdio.h>
#include <string.h>

struct staffs{
    int id;
    char name[20];
    char gender;
    int age;
    char address[50];
};

int main()
{
    struct staffs no1;
    no1.id = 1;
    strcpy(no1.name, "王小明");
    no1.gender = 'm';
    no1.age = 23;
    strcpy(no1.address, "xx市xx区xx里xx路xx号x楼");

    struct staffs no2;
    no2.id = 2;
    strcpy(no2.name, "叶花花");
    no2.gender = 'w';
    no2.age = 26;
    strcpy(no2.address, "oo市oo区oo里oo路o号o楼");

    printf("---列印员工资料---\n\n");

    printf("员工编号: %d\n", no1.id);
    printf("姓名: %s\n", no1.name);
    printf("性别: ");
    if(no1.gender == 'm'){
        printf("男\n");
    }
    else if(no1.gender == 'w'){
        printf("女\n");
    }
    else{
        printf("不详\n");
    }
    printf("年纪: %d\n", no1.age);
    printf("地址: %s\n", no1.address);

    printf("---列印员工资料---\n\n");

    printf("员工编号: %d\n", no2.id);
    printf("姓名: %s\n", no2.name);
    printf("性别: ");
    if(no2.gender == 'm'){
        printf("男\n");
    }
    else if(no2.gender == 'w'){
        printf("女\n");
    }
    else{
        printf("不详\n");
    }
    printf("年纪: %d\n", no2.age);
    printf("地址: %s\n", no2.address);

    return 0;
}

https://ithelp.ithome.com.tw/upload/images/20211202/2014256544nqVV3bg2.png


小结

  1. struct可以包含许多不同资料型态的成员。

  2. 在进行struct定义後,仍需进行变数宣告才可以使用。

  3. 在进行结构的变数宣告时,要注意资料型态是什麽、有没有取别名。


看到这里就大致介绍完struct了,不晓得大家有没有办法吸收进去呢?内容有使用到第十五篇介绍的string函式库,大家如果有不了解的地方,可以多多尝试或查其他资料!

下一篇我们来介绍c语言怎麽进行读档、写档!


<<:  为了转生而点技能-JavaScript,day13(this上篇:简易呼叫及物件的方法呼叫

>>:  选择动物页面

人脸辨识的流程--特徵撷取

人脸辨识系统有三个步骤,人脸侦测、特徵撷取、人脸识别。 特徵撷取(Feature extractio...

在日本登记公司跟运营公司用的服务

今天分享我们在日本登记公司跟运营公司用的一些服务 虚拟住址、电话、电话秘书 登记公司需要住址跟电话 ...

铁人赛Day30-第九章:动画5.2-天气与湾熊,今天一次完成它!

不知不觉来到了最後一天,虽然每天要挤出时间做动画不容易,但做的时候以及解决一个难题的时候都会很开心地...

Day30:Azure小白如何使用Azure Active Directory Identity protection管好管满

在昨天我们谈完Azure小白想早下班-之-使用Azure Synapse Analytics汇入数P...

[DAY-24] 突破生产障碍 高效产出

你是否可以升迁 看的是你替组织带来贡献的可能性 超出期待的方法 就是朝大方向前进(BIG PICT...