Day26 C语言基础 结构体

目录

1.结构体的构造类型

2.定义结构

3.引用

 4.嵌套

 5.结构体指针

 6.结构体数组

 7.结构体与函数


1.结构体的构造类型

不是基本类型也不是指针,他是若干相同或不同数据的集合
常用的构造类型有数据、结构体、共用体。
数组是具有相同类型的数据的集合,用于处理大量相同类型的数据运算
结构体的概念:结构体是一种构造类型的数据结构

2.定义结构

在定义结构体类型时顺便定义结构体变量,以后还可以定义结构体变量
        定义结构体类型+结构体变量

struct Books

{        

                        char title[50];

                        char author[50];

                        char subject[100];

                        int book_id;

} book;

注意:        定义结构体类型时,不分配空间
                   定义结构体变量后,才会分配空间

第一种定义方式 

//定义一个结构体类型名 
struct stu_info
{
	int id;//学号
	char name[20]; //保存学生的姓名
	int age;  //保存学生的年龄
	char addr[30];//保存学生的地址 
}wangwu = {1001,"wanwu",23,"xian bowen road",};//第一种方法:可以在定义结构体类型名是定义结构体变量 
//第一种初始化的方式 

 第二种定义方式

//第二种方法:定义两个结构体变量才会开辟空间 //第二种方法 
int main()
{	
    struct stu_info  zhangsan = {1002,"zhangsan",24,"xian jinye road"},lisi;
}

第三种定义方式

//第三种初始化方法 
	 struct stu_info xiaoming = {
		 .id = 1003,
		 .name = "xiaoming",
		 .age = 25,
		 .addr = "xian bowen raod"
	 };

typedef

 主要用于给一个类型取别名,此时相当于给当前结构体重新骑了一个类型名A,相当于struct结构体名,所以如果结构体要取别名,一般不需要给结构体定义名字,以后定义变量是,直接使用A即可,不用加struct。

typedef struct stu
{
    int id;
    char name[20];
    int age;
    float score;
}STU;
    STU stu = {1001,"lilei",24,98.5};//定义一个结构体变量

结构体的初始化及应用
    结构体变量是个变量,这个变量是若干个数据的集合
    1.在定义结构体变量之前,必须先定义结构类型,然后在定义变量
    2.在定义结构体变量时,可以顺便给结构体变量赋初值,称为初始化
    3.结构体变量初始化时,各个成员顺序初始化 

3.引用

初始化结构体变量后,就可以像普通变量一样,进行各种操作

zhangsan.age++;
lisi.age = wangmazi.age;
zhangsan = lisi;

 4.嵌套

结构体嵌套使用:成员引用时不能跳级,要一级一级使用

#include<stdio.h>
#include<string.h>
 
typedef struct birthday
{
	int year;
	int month;
	int day;	
}BIR;
 
struct person
{
	int id;
	char name[20];
	int age;
	BIR birth;	
};
 
int main()
{
	struct person per1;
	per1.id = 1001;
	strcpy(per1.name, "李雷"); 
	per1.age = 23;
	per1.birth.year = 1998;
	per1.birth.month = 1;
	per1.birth.day = 27;
	
	printf("学号:%d\n姓名:%s\n年龄:%d\n生日:%d年%d月%d日\n", per1.id, per1.name, per1.age, per1.birth.year, per1.birth.month, per1.birth.day);	
} 

 5.结构体指针

结构体指针访问他指向的变量的成员用->去访问 

#include<stdio.h>
#include<string.h>
 
typedef struct stu
{
	int id;
	char name[20];
	int age;
	float score;
}STU;
 
int main()
{
	STU stu = {1001,"lilei",24,98.5};//定义一个结构体变量
	printf("%d,%s,%d,%.1f\n",stu.id,stu.name,stu.age,stu.score);
	STU *pstu;//定义一个结构体指针
	pstu = &stu; //结构体指针访问他指向的变量的成员用->去访问 
	pstu->id = 1002;
	strcpy(pstu->name,"zhangsan");
	pstu->age  = 25;
	pstu->score = 92;
	printf("%d,%s,%d,%.1f\n",pstu->id,pstu->name,pstu->age,pstu->score);
	printf("%d,%s,%d,%.1f\n",stu.id,stu.name,stu.age,stu.score);
	return 0;
}

 6.结构体数组

定义个结构体数组,来批量位结构体变量赋值,这样省去了很多代码量

#include<stdio.h>
#include<string.h>
 
typedef struct stu
{
	int id;
	char name[20];
	float score;
}STU;
 
int main()
{
	STU stu[3];
	for(int i = 0; i < 3; i++)
	{
		scanf("%d%s%f", &stu[i].id, stu[i].name, &stu[i].score);	
	}
	
	for(int i = 0; i < 3; i++)
	{
		printf("%d, %s, %f", stu[i].id, stu[i].name, stu[i].score);
	} 
	
	return 0;
}

 7.结构体与函数

结构体作为函数的入参时,也是单向传值

#include<stdio.h>
#include<string.h>
 
typedef struct
{
	int id;
	char name[20];
	float score;
}STU;
//结构体中的成员作为函数的参数 
float compare_score(float a, float b);
STU compare(STU stu1, STU stu2);ha 
int main()
{
	STU stu1 = {1001, "李雷", 98.5}, stu2 = {1002, "韩梅梅", 89};
	float ret = compare_score(stu1.score, stu2.score);
	printf("%.1f\n", ret);
	STU ret1 = compare(stu1, stu2);
	printf("%d, %s, %.1f", ret1.id, ret1.name, ret1.score); 
	return 0;
}
float compare_score(float a, float b)
{
	if(a > b)
	{
		return a;
	}
	else
	{
		return b;
	}
}
 
STU compare(STU stu1, STU stu2)
{
	if(stu1.score > stu2.score)
	{
		return stu1;
	}
	else
	{
		return stu2;
	}
}

返回结构体,就用结构体类型名当作返回值类型,返回的就是结构体

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕容离875

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值