C++结构体全集

本文详细介绍了C++中的结构体使用,包括定义、创建结构体变量、结构体数组、结构体指针、结构体嵌套以及结构体作为函数参数的方法,并通过实例演示了各种用法,同时提到了const在结构体中的应用,以防止意外修改实参。
摘要由CSDN通过智能技术生成

内容概览

文章介绍了C++结构体的定义使用、结构体数组、结构体指针、结构体嵌套结构体、结构体做函数参数、结构体中const的使用等知识点,有兴趣的可以瞄一眼。

一、结构体定义和使用

定义语法:struct 结构体名 { 结构体成员列表 };
三种创建方式:
1、struct 结构体名 变量名;
2、struct 结构体名 变量名={ … };
3、定义时分号前添加变量名:struct 结构体名 { 结构体成员列表 } 变量名;
注意:方式二创建为常用方法,创建时struct可省略

// training code
#include <string>
#include <iostream>
using namespace std;

//define struct
struct student
{
	string name;
	int age;
	float score;
}s3;
int main()
{
	//creat way1:struct student s1;
	struct student s1;
	s1.name = "tom";
	s1.age = 18;
	s1.score = 255;

	cout << "name:" << s1.name << "  age:" << s1.age << "  score:" << s1.score << endl;

	//creat way2:struct student s2={...};
	struct student s2 = { "jack",18,160 };
	cout << "name:" << s2.name << "  age:" << s2.age << "  score:" << s2.score << endl;

	//creat way3:struct student{string name;int age;float score;}s3;
	s3.name = "monkey";
	s3.age = 16;
	s3.score = 120;
	cout << "name:" << s3.name << "  age:" << s3.age << "  score:" << s3.score << endl;

	system("pause");
	return 0;
}

二、结构体数组

1、定义结构体,同上
2、创建结构体数组:struct 结构体名 数组名[元素个数]={{…},{…}…};
3、可对结构体数组元素进行操作

// struct array
#include <string>
#include <iostream>
using namespace std;

//define struct
struct student
{
	string name;
	int age;
	float score;
};
int main()
{
	//creat struct array
	struct student atuArray[3] = 
	{
		{"first",18,25},{"second",23,190}
	};

	//assign valua to member
	atuArray[2].name = "third";
	atuArray[2].age = 66;
	atuArray[2].score = 99;

	for (int i = 0; i < 3; i++)
	{
		cout << atuArray[i].name << "  age:"<<atuArray[i].age << "  score:"<<atuArray[i].score << endl;
	}
	system("pause");
	return 0;
}

三、结构体指针

1、定义结构体,语法同一
2、创建结构体元素,语法同一
3、创建指针并赋予元素地址
注意:指针类型需定义为所定义的结构体数据类型,如:struct student,访问结构体指针元素属性时需用 -> 符号访问

// struct pionter
#include <string>
#include <iostream>
using namespace std;

//define struct
struct student
{
	string name;
	int age;
	float score;
};
int main()
{
	//creat struct member
	struct student s = {"liubei",64,60};
	//creat a pionter and assign the address to it
	struct student *p = &s;

	cout << "name:" << p->age << "  age:" << p->age << "  score:" << p->score << endl;
	system("pause");
	return 0;
}

四、结构体嵌套结构体

嵌套结构体,即在结构体A 中以结构体B作为属性,使用时,B须在A前面定义,然后在A中创建

// nesting struct
#include <string>
#include <iostream>
using namespace std;

//define student struct
struct student
{
	string name;
	int age;
	float score;
};

//define teacher struct
struct teacher
{
	string id;
	string name;
	int age;
	struct student s;

};
int main()
{
	//creat struct member
	struct teacher t = { "16001","mr.wang",60,{"xiaoli",16,99} };

	cout << "tewacher's id:" << t.id << "  teacher's name:" << t.name << "  teacher's age:" << t.age << endl;
	cout<<"student's name" << t.s.name << "  student's age:"<<t.s.age<<"student's score:"<<t.s.score<<endl;
	system("pause");
	return 0;
}

五、结构体做函数参数

结构体作为函数参数方法有两种
1、值传递:修改形参不会改变实参,仅仅复制数据至函数
2、地址传递:实参会随形参的改变而改变,并且可以节省空间

// using structer as parameter
#include <string>
#include <iostream>
using namespace std;

//define student struct
struct student
{
	string name;
	int age;
	float score;
};
//pass valua
void printstudent(struct student s)
{
	cout << "p_name:" << s.name << "  p_age:" << s.age << "  p_score:" << s.score << endl;
}
//pass address
void printstudent2(struct student *p)
{
	cout << "p2_name:" << p->name << "  p2_age:" << p->age << "  p2_score:" << p->score << endl;
}

int main()
{
	struct student s = { "xiaoming",20,125 };
	printstudent(s);
	printstudent2(&s);
	//cout << "name:" << s.name << "  age:" << s.age << "  score:" << s.score << endl;
	system("pause");
	return 0;
}

六、结构体中const的使用

当结构体采用地址传值时,形参的改变会影响实参,为了防止误操作修改实参,在函数体定义时增加const关键字,使函数内部无法对参数进行操作

// using const to limit operation
#include <string>
#include <iostream>
using namespace std;

//define student struct
struct student
{
	string name;
	int age;
	float score;
};


//If operate on the pointer,  an error will be reported
void printstudent2(const struct student *p)
{
	p->name = "try";
	cout << "p2_name:" << p->name << "  p2_age:" << p->age << "  p2_score:" << p->score << endl;
}

int main()
{
	struct student s = { "xiaoming",20,125 };
	printstudent2(&s);
	system("pause");
	return 0;
}

以上。
文章以简洁行文梳理结构体知识点,如有疑问可留言详细探讨。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值