当前位置: 首页 > news >正文

C语言基础11——结构体1

结构体1

1. 结构体

1.1 定义

用户自定义的数据类型,在结构体中可以包含若干不同数据类型(也可以相同)的成员变量,使这些数据项组合起来反应某一个信息

1.2 格式

struct 结构体名 (用户自定义的数据类型)

{

        数据类型 成员变量1;

        数据类型 成员变量2;

        数据类型 成员变量3;

        ...

};

1.3 结构体变量

1.3.1 概念

通过结构体类型定义的变量

1.3.2 格式

struct 结构体名 变量名;

1.3.3 先定义结构体类型,再结构体变量

struct 结构体名

{

        成员变量;

};

struct 结构体名 变量名;

#include <stdio.h>struct student
{char name[32];int id;int score;int age;
};int main()
{struct student stu;return 0;
}

1.3.4 定义结构体的同时,定义结构体变量

#include <stdio.h>struct student
{char name[32];int id;int score;int age;
} stu;int main()
{return 0;
}

1.3.5 缺省结构体名定义结构体变量

#include <stdio.h>struct
{char name[32];int id;int score;int age;
} stu;int main()
{return 0;
}

1.4 赋值

1.4.1 定义变量的同时直接大括号赋值 (初始化)

注意:此时给结构体变量赋值的时候,要按照定义变量的顺序进行赋值,否则会导致数据错误或者赋值数据类型错误。

#include <stdio.h>struct student
{char name[32];int id;int score;int age;
};int main()
{struct student stu = {"zhangsan", 12138, 80, 18};return 0;
}

1.4.2 定义变量时未初始化,然后对成员变量单独赋值

对变量单独赋值,需要对定义的结构体变量用变量名找到结构体内部定义的变量进行赋值,格式为:结构体名.变量名,此时的赋值可以不再按照定义时候的顺序赋值。

#include <stdio.h>
#include <string.h>struct student
{char name[32];int id;int score;int age;
};int main()
{struct student stu;strcpy(stu.name, "lisi");stu.id = 12139;stu.score = 88;stu.age = 20;return 0;
}

1.4.3 点等法赋值

#include <stdio.h>struct student
{char name[32];int id;int score;int age;
};int main()
{struct student stu = {.name = "wangwu",.id = 12140,.score = 0,.age = 20};return 0;
}

1.5 访问

通过 .访问:结构体变量名.成员变量名

        scanf("%s %d %d %d", stu.name, &stu.id, &stu.score, &stu.age);

        printf("%s %d %d %d\n", stu.name, stu.id, stu.score, stu.age);

练习1:创建一个名为student的结构体,包含姓名,学号,班级,分数,(数据类型自己定义),从终端输入学生的信息并打印。(参考代码见本文最后)

1.6 重定义 typedef

typedef int int _num;                //int == int_num

int a; == int_num a;

typedef int * int _p;                   //int * == int_p

int *p = &a;                               //== int _p p = &a;

1.6.1 定义结构体的同时重定义

typedef struct student

{

        int id;

        int score;

} STU; // STU是结构体类型重定义的名字

struct student stu; == STU stu;

1.6.2 先定义结构体,然后重定义

struct student

{

        int id;

        int age;

};

typedef struct student STU;

2. 结构体数组

2.1 概念

结构体类型相同的变量组成的数组

2.2 格式

2.2.1 定义结构体的同时定义结构体数组

#include <stdio.h>
#include <string.h>struct student
{char name[32];int id;int score;int age;
}stu[3];int main()
{return 0;
}

2.2.2 先定义结构体,然后定义结构体数组

#include <stdio.h>
#include <string.h>struct student
{char name[32];int id;int score;int age;
};int main()
{struct student stu[3];return 0;
}

2.3 赋值

2.3.1 定义结构体数组的同时赋值

#include <stdio.h>
#include <string.h>struct student
{char name[32];int id;int score;int age;
};int main()
{struct student stu[3] = {{"zhangsan", 12138, 80, 18},    //第一个人的信息{"lisi", 12139, 92, 20},        //第二个人的信息{"wangwu", 12140, 0, 21}        //第三个人的信息};return 0;
}

2.3.2 先定义结构体数组。再对结构体数组的每一个人元素分别赋值

与普通数组赋值方式相比就是多了个内部变量访问,也是需要通过数字下标找到第几个人

#include <stdio.h>
#include <string.h>struct student
{char name[32];int id;int score;int age;
};int main()
{struct student stu[3];strcpy(stu[0].name, "zhangsan");stu[0].id = 12138;strcpy(stu[1].name, "lisi");stu[1].id = 12139;return 0;
}

2.4 结构体数组的输入输出:(for循环遍历)

#include <stdio.h>
#include <string.h>struct student
{char name[32];int id;int score;int age;
};int main(int argc, char const *argv[])
{struct student stu[3];for(int i = 0; i < 3; i++){scanf("%d %d %d %s",&stu[i].id, &stu[i].score, &stu[i].age, &stu[i].name);printf("%d %d %d %s\n",stu[i].id, stu[i].score, stu[i].age, stu[i].name);}return 0;
}

------------------------------------------------------这是一条分割线------------------------------------------------------

天才预设的上一篇跳转路径:C语言基础10——函数-CSDN博客

天才预设的下一篇跳转路径:未完待续~

天才预设的合集传送路径:C基础_Gu_shiwww的博客-CSDN博客

参考答案

练习1:

创建一个名为student的结构体,包含姓名,学号,班级,分数,(数据类型自己定义),从终端输入学生的信息并打印。

#include <stdio.h>typedef struct student
{int id;char name[10];int sorce;
}STU;void swap(STU *a,STU *b)
{STU c;c  = *a;*a = *b;*b = c ;
}void sort1(STU a[],int m)
{for(int i = 0 ; i < m - 1 ; i++){for(int j = 0 ; j < m - 1 ; j ++){if(a[j].sorce > a[j + 1].sorce){swap(&a[j], &a[j + 1]);}}}
}int main()
{STU stu[5];for(int i = 0 ; i < 5 ; i ++){scanf("%d %s %d", &stu[i].id, stu[i].name, &stu[i].sorce);}sort1(stu,5);for(int i = 0 ; i < 5 ; i ++){printf("%d %s %d\n", stu[i].id, stu[i].name, stu[i].sorce);}return 0;
}

http://www.dtcms.com/a/306969.html

相关文章:

  • Qt Quick 动画与过渡效果
  • QT中QTableView+Model+Delegate实现一个demo
  • TikTok 视频审核模型:用逻辑回归找出特殊类型的视频
  • 全栈:SSH和SSM和Springboot mybatisplus有什么区别?
  • 以ros的docker镜像为例,探讨docker镜像的使用
  • 力扣刷题日常(7-8)
  • 【Arch-Linux,hyprland】常用配置-已实验成功指令大全(自用)(持续更新)
  • 如何保证数据库的持久性与一致性:从 Linux 磁盘缓存策略到 MySQL 的设计
  • 执业药师证识别技术:医药健康生态中发挥愈发关键的作用
  • 微软:科技领域的创新巨头
  • Sleeping Cup 论坛:连接开发者与创新的桥梁
  • 隧道COVI检测器的用处
  • [SKE]使用OpenSSL库实现AES、SM4、DES、RSA、3DES_EDE和3DES_EEE算法的加解密验证
  • SringBoot入门
  • Linux启动防火墙提示提示 Active: failed (Result: timeout)
  • Golang 指针与引用深度解析:对比 C/C++ 的内存管理哲学
  • Jupyter Notebook安装使用
  • Javascript对象合并
  • Centos7 | 防火墙(firewalld)使用ipset管理ip地址的集合
  • MySQL 读写分离(含示例代码)
  • 新注册企业信息查询“数据大集网”:驱动企业增长的源头活水
  • 10 卷积神经网络
  • LLMs之Agent:GLM-4.5的简介、安装和使用方法、案例应用之详细攻略
  • 51单片机入门:数码管原理介绍及C代码实现
  • 【硬件】元器件选型
  • 【ESP32设备通信】-LAN8720与ESP32集成
  • 订阅区块,部署合约,加载合约
  • Akamai CloudTest before 60 2025.06.02 XXE注入导致文件包含漏洞(CVE-2025-49493)
  • MOEA/DD(多目标进化算法基于分解)简介
  • AAAI‘26 | 聚焦人工智能前沿:西工大李学龙教授荣任赞助主席,论文取号逼近三万,精彩不容错过!