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

C语言基础之结构体

结构体

可以理解为自定义的数据类型,由一批数据组合而成的结构型数据,里面的每一个数据都是结构体的"成员"。

格式:

struct 结构体名字
{    成员1;成员2;...
};

例子1:

#include<stdio.h>
#include<string.h>
//创建一个结构体
struct girlFriend
{char name[100];int age;char gender;double height;
};
int main() {//定义一个结构体变量struct girlFriend gf1;//赋值strcpy(gf1.name, "小诗诗");gf1.age = 23;gf1.gender = 'F';gf1.height = 1.89;printf("姓名:%s\n", gf1.name);printf("年龄:%d\n", gf1.age);printf("性别:%c\n", gf1.gender);printf("身高:%lf\n", gf1.height);return 0;
}

例子2:

#include<stdio.h>
//定义一个结构体
struct Student
{char name[100];int age;
};
//定义三个学生,把他们的信息放入数组中,遍历数组
int main() {struct Student stu1 = { "zhangsan",23 };struct Student stu2 = { "lisi",24 };struct Student stu3 = { "wangwu",25 };struct Student stuArr[3] = { stu1, stu2, stu3 };for (int i = 0; i < 3; i++) {struct Student temp = stuArr[i];printf("姓名:%s, 年龄:%d\n", temp.name, temp.age);}return 0;
}

给结构体起别名

格式:

typedef struct (结构体名[可以不写])
{成员1;成员2;...
}别名;

例子:

#include<stdio.h>
//定义一个结构体,并给他起别名
typedef struct
{char name[100];int attack;int defense;int blood;
}M;
int main() {M taro = { "泰罗", 100, 90, 500 };M rem = { "雷欧", 90, 80, 4450 };M eddie = { "艾迪", 120, 79, 600 };M arr[3] = { taro, rem, eddie };for (int i = 0; i < 3; i++) {M temp = arr[i];printf("姓名:%s, 攻击力:%d, 防御力:%d, 血量:%d\n", temp.name, temp.attack, temp.defense, temp.blood);}return 0;
}

结构体作为函数参数:

也就是函数中传递结构体

分为如下两种情况:

1,传递结构体中的数据值(不能真正修改原数据值

2,传递结构体的地址值

例子:

#include<stdio.h>
#include<string.h>
//结构体作为函数参数
typedef struct
{char name[100];int age;}S;
//定义函数修改结构体中的数据
void method(S* p) {printf("接收到的数据:%s, %d\n", (*p).name, (*p).age);//输入修改了的数据scanf("%s", (*p).name);scanf("%d", &(*p).age);printf("修改后的数据: % s, %d\n", (*p).name, (*p).age);
}
int main() {S stu;strcpy(stu.name, "aaa");stu.age = 0;printf("初始化的数据:%s, %d\n", stu.name, stu.age);method(&stu);printf("修改后的数据: % s, %d\n", stu.name, stu.age);
}

结构体嵌套

就是说如果结构体中成员的类型是其他的结构体,这就使用了结构体的嵌套

例子:

//结构体Student:名字,年龄,性别,身高,联系方式
//结构体联系方式(Message):手机号,电子邮箱
#include<stdio.h>
#include<string.h>
struct Message
{char phone[12];char mail[100];
};
struct Student
{char name[100];int age;char gender;double height;struct Message msg;
};
int main() {//struct Student stu;/*strcpy(stu.name, "LiSi");stu.age = 23;stu.gender = 'M';stu.height = 1.79;strcpy(stu.msg.phone, "123456");strcpy(stu.msg.mail, "1278987489@qq.com");*///批量赋值:struct Student stu2 = { "LiSi",24,'F',1.89, {"1236473","434768714.qq.com"} };//输出/*printf("姓名:%s\n", stu.name);printf("年龄:%d\n", stu.age);printf("性别:%c\n", stu.gender);printf("身高:%lf\n", stu.height);printf("手机号:%s\n", stu.msg.phone);printf("邮箱:%s\n", stu.msg.mail);*/printf("姓名:%s\n", stu2.name);printf("年龄:%d\n", stu2.age);printf("性别:%c\n", stu2.gender);printf("身高:%lf\n", stu2.height);printf("手机号:%s\n", stu2.msg.phone);printf("邮箱:%s\n", stu2.msg.mail);return 0;
}
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
struct spot
{char name[10];int count;
};
int main() {struct spot arr[4] = { {"A", 0}, {"B", 0}, {"C", 0}, {"D", 0} };//遍历for (int i = 0; i < 4; i++) {struct spot temp = arr[i];printf("%s %d\n", temp.name, temp.count);}//模拟80人投票int max = arr[0].count;srand(time(NULL));for (int i = 0; i <= 80; i++) {int choose = rand() % 4;// 0 1 2 3arr[choose].count++;//找最大值for (int j = 1; j < 4; j++) {struct spot temp = arr[j];if (temp.count > max) {max = temp.count;}}}printf("%d\n", max);for (int i = 0; i < 4; i++) {struct spot temp = arr[i];if (temp.count == max) {printf("投票最多的景点为:%s, 共计:%d", temp.name, temp.count);break;}}return 0;}

结构体内存对齐:

1.确定变量的位置:只能放在自己数据类型含有字节数的整数倍的内存地址上

2.最后一个补位:结构体的总大小,是最大类型的整数倍

如图:

0a
1a
2a
3a
4a
5a
6a
7a
8b
9
10
11
12c
13c
14c
15c
16
17
18
19
20
21
22
23
24d

 

#include<stdio.h>
struct num {double a;char b;int c;char d;
};
int main() {struct num n;printf("%zu\n", sizeof(n));//24return 0;
}

共用体

概念:一种数据可能有多种类型

特点:

1.共用体,也叫联合体,共同体

2.所有变量都使用同一个内存空间

3.所占的内存大小= 最大成员长度(也受内存对齐的影响)

4.每次只能给一个变量赋值,再次赋值会覆盖原有数据

格式:

union Money
{double mond;int moni;char mons;
};
union Money m;
m.moni = 100;
printf("%d\n",m.moni);//100起别名:
格式:
typedef union Money
{double mond;int moni;char mons;
}mt;
mt money;
money.moni = 100;
printf("%d\n",m.moni);//100

例子:

#include<stdio.h>
union MoneyType
{int moneyi;double moneyd;char moneystr[100];
};
int main() {union MoneyType money;//三个地址结果一样printf("%p\n", &(money.moneyi));printf("%p\n", &(money.moneyd));printf("%p\n", &(money.moneystr));money.moneyi = 99;money.moneyd = 1.23;printf("%lf\n", money.moneyd);printf("%lf\n", money.moneyi);//出错了return 0;
}运行结果:
000000F58E1FF920
000000F58E1FF920
000000F58E1FF920
1.230000
0.000000

结构体和共同体的区别:

存储方式:

        结构体:自己存自己的

        共同体:存在一起,多次存会覆盖

内存占用:

        结构体:各个变量的总和(受内存对齐影响)

        共同体:最大类型(受内存对齐影响)

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

相关文章:

  • 西安做网站的公司地址王烨捷
  • 数据风险评估与安全风险评估的核心解析
  • Milvus:标量字段-字符串、数字、数组与结构数组(七)
  • 怎样做英文网站wordpress修改中文
  • 计算机网络学习笔记 | 传输层核心知识点总结(DAY03,匠心制作)
  • 做团购的的网站有哪些ug.wordpress.org
  • 从 CAD 图纸到 Excel 数据:橙色云智橙 PLM 打造制造企业数字化协同新模式
  • 【openGauss】构建一个兼容Oracle模式支持创建package的openGauss的docker镜像
  • 广州地区网站建设做的好的农产品网站
  • 城市本地生活实体零售可信数据空间 RWA 平台方案
  • 接管所有System.out.println转成Logger输出日志
  • 建三江廉政建设网站长春网站制作推广
  • [LitCTF 2023]这是什么?SQL !注一下 !
  • 小数位进制转换怎么用 python 表示
  • 网站页面报价镇海区建设工程安监站网站
  • ESP32 HTTP回调机制详解与优化实践
  • Pycharm+Deepseek结合使用Continue插件无法返回中文产生乱码
  • k8s基础概念、Pod、k8s基础命令
  • 设计模式——适配器(adapter)
  • Js随堂笔记2025-11-3
  • 贵州网站建设设计公司哪家好怎么网站搜索排名优化
  • 使用 Docker Compose 轻松实现 INFINI Console 离线部署与持久化管理
  • 基于SpringBoot的高校社团管理系统【协同过滤推荐算法+数据可视化】
  • 中山网站建设文化流程免费网站建设市场
  • MDI Jade9.0中文版下载及详细安装教程,MDI Jade安装包(附详细教程)
  • hash滑窗|dp
  • 免费APP 微信 网站平台汕头网
  • 网页翻译用什么软件长春百度关键词优化
  • 【js逆向案例三】瑞数6
  • 等保三级“通关”秘籍:如何化繁为简,高效通过