学习:结构体

引言:今天打算把C++的基础全部都学一遍,然后明天继续学,感觉基础还是蛮好理解的,自己还想再学快一点,不知道什么时候能学到内存那边,我想实现免杀呢,感觉距离很遥远,冲冲冲。

结构体基本概念:
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型


结构体定义和使用:
语法:struct 结构体名 { 结构体成员列表 };

通过结构体创建变量的方式有三种:

1、struct 结构体名 变量名
2、struct 结构体名 变量名 = { 成员1值 , 成员2值...}
3、定义结构体时顺便创建变量

#include<iostream>
#include<iostream>
#include<string>
using namespace std;

struct Student {
	string name;
	int age;
	int score;
}s3; //第三种定义方法,这种方法我们不需要去创建Student


int main() {
	
	struct Student s1; //第一种定义方法
	s1.age = 18;
	s1.name = "adexx";
	s1.score = 100;
	cout << s1.age << "," << s1.name << "," << s1.score << endl;
	
	struct Student s2 = {"adexx",18,100}; // 第二种定义方法
	cout << s2.age << "," << s2.name << "," << s2.score << endl;


	s3.age = 18;
	s3.name = "adexx";
	s3.score = 100;
	cout << s2.age << "," << s2.name << "," << s2.score << endl;
	
	
	system("pause");
	return 0;
}

总结1:定义结构体时的关键字是struct,不可省略

总结2:创建结构体变量时,关键字struct可以省略

总结3:结构体变量利用操作符 ''.'' 访问成员


结构体数组:

作用:将自定义的结构体放入到数组中方便维护。

语法: struct 结构体名 数组名[元素个数] = { {} , {} , ... {} }

#include<iostream>
#include<string>
using namespace std;


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


int main() {

	struct student arr[3] =
	{
		{"张三",18,80 },
		{"李四",19,60 },
		{"王五",20,70 }
	};

	for (int i = 0; i < 3; i++) {
		cout << arr[i].age << "," << arr[i].name << "," << arr[i].score << endl;
	}

	system("pause");
	return 0;
}

结构体指针:

作用:通过指针访问结构体中的成员

#include<iostream>
#include<string>

using namespace std;

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

int main() {

	struct student s1 = { 11,100,"adexx" };
	student * p = &s1;
	cout << p->name << "," << p->age << "," << p->score << endl;
	system("pause");
	return 0;


}

总结:结构体指针可以通过 -> 操作符 来访问结构体中的成员
困惑:为什么当指针存储s1的内存地址的时候,访问的时候就需要->,而本身结构体实例的时候就用.来访问。


结构体嵌套结构体:

**作用: **结构体中的成员可以是另一个结构体

#include<iostream>
#include<string>
using namespace std;


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

struct teacher {
	string name;
	int age;
	struct student stu;
};

int main() {

	struct teacher t1;
	
	t1.name = "yu";
	t1.age = 28;
	t1.stu.age = 18;
	t1.stu.score = 100;
	t1.stu.name = "adexx";

	cout << t1.stu.age << "," << t1.stu.score << "," << t1.stu.name << "," << t1.name << "," << t1.age << "," << endl;

	system("pause");
	return 0;
}

总结:在结构体中可以定义另一个结构体作为成员,用来解决实际问题


结构体做函数参数:

作用:将结构体作为参数向函数中传递

传递方式有两种:

1、值传递
2、地址传递

#include<iostream>
#include<string>
using namespace std;


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

//值传递
void print_student_zhichuandi(struct student s) {
	s.age = 100;
	cout << s.name << "," << s.age << "," << s.score << endl;
}

//地址传递
void print_student_dizhichuandi(struct student * p){
	p->age = 100;
	cout << p->name << "," << p->age << "," << p->score << endl;
}

int main() {
	
	struct student s1 = { "adexx", 18 ,100 };
	//print_student_zhichuandi(s1); // 调用值传递
	print_student_dizhichuandi(&s1); // 因为传递的是地址,所以函数的接收的需要是个指针,既然传递是指针,那么也就需要&

	cout << s1.name << "," << s1.age << "," << s1.score; //最后打印出来的数据的不同 主要还是看函数的参数是值传递还是地址传递
	
	

	system("pause");
	return 0;
}

总结:如果不想修改主函数中的数据,用值传递,反之用地址传递


结构体中 const使用场景:

作用:用const来防止误操作

在https://www.cnblogs.com/zpchcbd/p/11841223.html中提到过const修饰指针

在const修饰的是指针,指针指向可以改,指针指向的值不可以更改,这里来达到const防止误操作,就可以用到这个

#include<iostream>
#include<string>
using namespace std;


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

void to_c_student(const student * s) {
	// s->age = 100;  这种是不成功的,因为const修饰指针 误操作
	cout << s->name << "," << s->age << "," << s->score;

}

int main() {

	struct student s = {"adexx", 18,100};
	to_c_student(&s);

	system("pause");
	return 0;
}
原文地址:https://www.cnblogs.com/zpchcbd/p/11841703.html