C语言基础之结构体

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 3
		arr[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));//24
	
	return 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

结构体和共同体的区别:

存储方式:

        结构体:自己存自己的

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

内存占用:

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

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值