C语言学习之结构体

栏目: C · 发布时间: 6年前

内容简介:一直以来,C语言的学习都在入门阶段,只用到数组、函数、循环、选择、位运算这些基本的知识,较少用到指针、预处理、结构体、枚举类型、文件操作等这些C语言的精髓内容,现在想想真不敢说自己C语言的结构体实际上就是一种特殊的数据类型,只不过这种数据类型包括了很多个基本类型的数据,如int、float、char等等,如在开发学生成绩管理系统时,有学号、姓名、院系、班级、年龄、各个科目、各个科目对应的成绩等,不使用结构体我们需要定义许多的变量,而使用结构体可以大大简化程序的设计,

前言

C语言学习之结构体

一直以来,C语言的学习都在入门阶段,只用到数组、函数、循环、选择、位运算这些基本的知识,较少用到指针、预处理、结构体、枚举类型、文件操作等这些 C语言 的精髓内容,现在想想真不敢说自己 熟练掌握C语言的使用 ,所以最近几篇文章是关于这些内容的,一方面是巩固自己的C语言编程能力,另一方面也为以后学习C++做准备。

什么是结构体

C语言的结构体实际上就是一种特殊的数据类型,只不过这种数据类型包括了很多个基本类型的数据,如int、float、char等等,如在开发学生成绩管理系统时,有学号、姓名、院系、班级、年龄、各个科目、各个科目对应的成绩等,不使用结构体我们需要定义许多的变量,而使用结构体可以大大简化程序的设计,

结构体类型的定义

结构体的定义格式为:

struct 结构体名称
{
	结构体所包含的成员变量;
}

与C++不同的是,C语言的结构体只能包括成员变量,不能包含成员函数,但可以定义函数指针指向一个函数。

定义一个名为student的结构体类型,包含id、name、score这3个成员变量。

struct student
{
    int id;
    char *name;
    float score;
};

定义两个student的结构体变量:

struct student lucy, jack;

当然,也可以在定义结构体的同时定义结构体变量:

struct student
{
    int id;
    char *name;
    float score;
}lucy, jack;

即把变量名放在定义结构体的最后即可。

如果只需要定义lucy、jack两个结构体变量,后面其他地方都不需要再定义新的结构体变量,那么结构体名可以省略,不建议使用这种方式:

struct 
{
    int id;
    char *name;
    float score;
}lucy, jack;

这种写法看起来是稍微简单了一些,但后面无法再定义新的结构体变量。

结构体变量在内存中的存储

理论上和数组元素的存储非常类似,结构体变量的存储也是连续存储的,但在实际的编译器实现过程中,各个成员变量之间可能会存在缝隙。

结构体变量

【示例】:

#include "stdio.h"
#include "stdlib.h"

struct student
{
    int id;
    char *name;
    float score;
};

struct student jack;

int main(void)
{
    //成员的赋值
    jack.id = 1234;
    jack.name = "jack";
    jack.score = 89.5;

    printf("%s id: %d, score: %.1f \r\n", jack.name, jack.id, jack.score);		

    return 0;
}

【输出结果】:

jack id: 1234, score: 89.5

当然,也可以在定义的时候整体赋值:

struct student lucy, jack = {1233, "jack", 92.6};

但不可以在定义之后,单独整体赋值;

struct student lucy;
lucy = {1233, "lucy", 92.6};

这种整体赋值不允许,会报错。

结构体数组

当然也允许定义结构体数组:

struct student cls[5];

定义的数组可以不指定数组大小。

【示例】:

#include "stdio.h"
#include "stdlib.h"

struct student
{
    int id;
    char *name;
    float score;
};
//定义的同时整体赋值
struct student cls[5] =     //也可以不给出数据大小: cls[]
{
    {1001, "Li Lei", 88.4},
    {1002, "Zhang Wei", 79.3},
    {1003, "Wang Ming", 70.8},
    {1004,"Zhao Yang", 80.5},
    {1005,"Li Liang", 89.5}
};

int main(void)
{
    int i;
    int num_80 = 0; //分数大于80的人数
    float average = 0, sum = 0;     //平均分和总和

    for(i = 0; i < 5; i++)
    {
        sum += cls[i].score;
        if(cls[i].score > 80)
            num_80++;
    }
    average = sum / 5.0;

    printf("分数大于80的人数:%d,总平均分:%.1f", num_80, average);	
    return 0;
}

【输出结果】:

分数大于80的人数:3,总平均分:81.7

结构体指针变量

定义格式为:

struct student *p;

【示例一】:

#include "stdio.h"
#include "stdlib.h"

struct student
{
    int id;
    char *name;
    float score;
};
//定义的同时整体赋值
struct student *pstu, stu = {1234, "lucy", 90.4};

int main(void)
{
    pstu = &stu;        //*pstu 等同于 stu

    printf("id: %d, name: %s, score: %.1f \n", stu.id, stu.name, stu.score);
    printf("id: %d, name: %s, score: %.1f \n", (*pstu).id, (*pstu).name, (*pstu).score);
    printf("id: %d, name: %s, score: %.1f \n", pstu->id, pstu->name, pstu->score);  //指针变量引用成员变量

    return 0;
}

【输出结果】:

id: 1234, name: lucy, score: 90.4
id: 1234, name: lucy, score: 90.4
id: 1234, name: lucy, score: 90.4

【示例二】

#include "stdio.h"
#include "stdlib.h"

struct student
{
    int id;
    char *name;
    float score;
};

struct student *ps;
struct student cls[] =
{
    {1001, "Li Lei", 88.4},
    {1002, "Zhang Wei", 79.3},
    {1003, "Wang Ming", 70.8},
    {1004,"Zhao Yang", 80.5},
    {1005,"Li Liang", 89.5}
};

int main(void)
{
    int len = sizeof(cls) / sizeof(struct student);    //求数组长度
    printf("id \t name \t score \n");

    for(ps = cls; ps < cls + len; ps++)
    {
        printf("%d \t %s \t %.1f \n", ps->id, ps->name, ps->score);
    }
    return 0;
}

【输出结果】:

id       name    score
1001     Li Lei          88.4
1002     Zhang Wei       79.3
1003     Wang Ming       70.8
1004     Zhao Yang       80.5
1005     Li Liang        89.5

参考资料:

Jlink使用技巧系列文章:

欢迎大家关注我的个人博客

或微信扫码关注我的公众号

C语言学习之结构体

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Data Structures and Algorithms

Data Structures and Algorithms

Alfred V. Aho、Jeffrey D. Ullman、John E. Hopcroft / Addison Wesley / 1983-1-11 / USD 74.20

The authors' treatment of data structures in Data Structures and Algorithms is unified by an informal notion of "abstract data types," allowing readers to compare different implementations of the same......一起来看看 《Data Structures and Algorithms》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

html转js在线工具
html转js在线工具

html转js在线工具