c,c++——结构体详解

本文详细介绍了C++中的结构体,包括基本概念、结构体数组、结构体指针、嵌套结构体、结构体作为函数参数以及const在结构体中的使用场景。通过实例代码展示了这些概念的实际应用。
摘要由CSDN通过智能技术生成

目录

1.结构体基本概念 

代码示例:

 2.结构体数组

代码示例:

3.结构体指针 

代码示例:

4.结构体嵌套结构体 

代码示例:

 

5.结构体做函数参数 

代码示例:

 

6.结构体中const使用场景 

代码示例:

 


1.结构体基本概念 

468a470773bf4c6fac5cd3fce76f83a5.png

代码示例:

#include<bits/stdc++.h>
using namespace std;

struct student{
	string name;
	int age;
	int score;
}s3;//顺便创建结构体对象

int main()
{
	student s1;
	s1.name = "xiaoming";
	s1.age = 18;
	
	student s2 = {"xiaohong",19,99};
	cout << s2.name << ' ' << s2.age << endl;
	
	return 0;
}

 

7b165774e02844859761cb5e78988132.png

 2.结构体数组

821a14c8cd8843819b2505249a478493.png

代码示例:

#include<bits/stdc++.h>
using namespace std;

struct student{
	string name;
	int age;
	int score;
};

int main()
{
	student sarray[2] =
	{
		{"xiaoming",18,99},
		{"xiaohong",19,100}
	};
	
	cout << sarray[1].name << endl;
	sarray[1].name = "xiaogang";
	cout << sarray[1].name << endl;
	
	return 0;
}

 

3.结构体指针 

d9349b9631e84377b2497cf049af4e9b.png

代码示例:

#include<bits/stdc++.h>
using namespace std;

struct student{
	string name;
	int age;
	int score;
};

int main()
{
	student sarray[2] =
	{
		{"xiaoming",18,99},
		{"xiaohong",19,100}
	};
	
	student *p = sarray;
	cout << p -> name << ' ' << p -> age << ' ' << p -> score << endl;
	
	return 0;
}

 

df593043e06245af8538b059d22f9e61.png

4.结构体嵌套结构体 

a5a7a7358b28411d894a8eb9cf545dc0.png

79af7a5d84da4808868842911a59e345.png

代码示例:

#include<bits/stdc++.h>
using namespace std;

struct student{
	string name;
	int age;
	int score;
};

struct teacher{
	string name;
	int age;
	struct student s;
};
int main()
{
	teacher t;
	t.s.name = "ss";
	t.s.age = 19;

	cout << t.s.name << ' ' << t.s.age << endl;
	
	return 0;
}

 

 

5.结构体做函数参数 

30ee30b306154f34899acc7760cd80ca.png

代码示例:

#include<bits/stdc++.h>
using namespace std;

struct student{
	string name;
	int age;
	int score;
};

void printstudent(student s)
{
	cout << s.name << ' ' << s.age << ' ' << s.score;
}

//地址传递
void printstudent2(student *s)
{
	cout << s -> name << ' ' << s -> age << ' ' << s -> score;
}

int main()
{
	student s = {"xiaoming",18,89};
	printstudent(s);
	printstudent2(&s);
	return 0;
}

 

 

622436cf134e40c0aa0ae88bee1ef002.png

6.结构体中const使用场景 

66c0e4156b0b4efbb88353fef467ceca.png

代码示例:

#include<bits/stdc++.h>
using namespace std;

struct student{
	string name;
	int age;
	int score;
};

void printstudent(student s)
{
	cout << s.name << ' ' << s.age << ' ' << s.score;
}

//地址传递
//将函数中的形参改为指针,可以减少内存空间而且不会复制新的副本出来
//使用了const之后,不可修改s的值
void printstudent2(const student *s)
{
	cout << s -> name << ' ' << s -> age << ' ' << s -> score;
}

int main()
{
	student s = {"xiaoming",18,89};
	printstudent(s);
	printstudent2(&s);
	return 0;
}

 

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

柏箱

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

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

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

打赏作者

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

抵扣说明:

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

余额充值