当前位置: 首页 > 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.lryc.cn/news/605299.html

相关文章:

  • AutoSAR(MCAL) --- ADC
  • VoIP技术全面深度学习指南:从原理到实践的认知进化
  • 【GEO从入门到精通】生成式引擎与其他 AI 技术的关系
  • Linux ARM 平台 C 语言操作 Excel 文件的常用库与工具汇总(支持 xls 和 xlsx)
  • Linux基本指令,对路径的认识
  • SringBoot入门
  • uvm-tlm-sockets
  • 关于MyBatis 的懒加载(Lazy Loading)机制
  • 腾讯云市场排名
  • linux进程概念(三)进程状态
  • COZE 开源,新一代 AI Agent 本地部署一条龙
  • 借助 Wisdom SSH 的 AI 助手构建 Linux 开发环境
  • 2. Agent与 React流程
  • 智能Agent场景实战指南 Day 26:Agent评估与性能优化
  • 【面试场景题】随机立减金额计算
  • 三十四、【Linux常用工具】rsync+inotify实时同步演示
  • 游卡,快手26届秋招内推
  • Cortex-M处理器的优势?
  • 解决Nginx的HTTPS跨域内容显示问题
  • Linux日志管理和时钟同步配置指南
  • DFT设计中的不同阶段介绍
  • 【C语言类型转换坑】乘法溢出隐患发现与正确写法
  • 嵌入式系统分层开发:架构模式与工程实践(二)(创建任务篇(二))
  • CSS-in-JS 动态主题切换与首屏渲染优化
  • 人类语言驱动物理机制建模的AIVC
  • Zynq SoC 中断控制系统设计与实现:基于 GPIO 的中断驱动开发
  • 亚马逊Kiro重塑AI编程:从“氛围编码”到规范驱动的革命
  • 论文研读(2025 KDD):细粒度人体轨迹建模
  • Python多线程利器:重入锁(RLock)详解——原理、实战与避坑指南
  • C++代码题部分(1)