栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++ 基础知识-结构体

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++ 基础知识-结构体

c++ 基础知识-结构体 1.初始化结构体,结构体数组
#include   
#include   
using namespace std;
struct Student
{
	string name;
	int age;
};
int main()
{
	Student stuArr[] = {{"a",1},{"b",2},{"c",3}};
	stuArr[1].name = "d";
	stuArr[1].age = 4;
	for (int i = 0;i
		cout<<"name :"< 
2.结构体指针 
#include   
#include   
using namespace std;

struct Student
{
	string name;
	int age;
};
int main()
{
	Student stu;
	stu.name = "a";
	stu.age = 2;
	Student *p = &stu;
	//*p->name = "b";
	//*p->age = 5;
	cout<<"name :"<name<name 不是*p->name
	cout<<"age :"<age< 
3.结构体嵌套 
#include   
#include   
using namespace std;

struct Learn
{
	int math;
	int english;
};
struct Student
{
	string name;
	int age;
	struct Learn learn;
};

int main()
{
	Student stu;
	stu.name = "a";
	stu.age = 5;
	//Learn learn;
	stu.learn.english = 10;
	stu.learn.math = 20;
	cout< 
4.结构体作为函数参数 
#include   
#include   
using namespace std;

struct Student
{
	string name;
	int age;
};

//void CoutStruct(Student stu);//值传递,需要重新开辟空间,浪费内存
void CoutStruct(Student * stu);//指针,节省空间
//void CoutStruct(const Student * stu);//通常加const,保证输入不改变
int main()
{
	Student stu;
	stu.age = 19;
	stu.name = "a";
	Student * p = &stu;
	CoutStruct(p);
	cout<<"age: "<
//	stu.age = 30;
//	cout<<"age: "<
	stu->age = 30;
	cout<<"age: "<age<name<
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/857450.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号