Dark horse programmer's ingenuity = structure

1. Structure definition

2. Structure array

#include<iostream>
#include<string>
using namespace std;
struct stu
{
	string name;
	int age;
	int score;

};
int main()
{
	stu stu1[2] = {
		{"张三",18, 90},
		{"李四",18, 90}
	};
	for (int i = 0; i < 2; i++)
	{
		cout << "姓名: " << stu1[i].name << "年龄: " << stu1[i].age << "分数:" << stu1[i].score << endl;
	}
	
		system("pause");
		return 0;
}

3. Structure pointer

#include<iostream>
#include<string>
using namespace std;
struct stu
{
	string name;
	int age;
	int score;

};
int main()
{
	stu stu1 = {"张三",18, 90};
	stu* p = &stu1;
	cout<< "姓名:" << p->name << endl;
	system("pause");
	return 0;
}

When defining a pointer to a structure, note that the type of the pointer must be the same as the structure type. When using pointers to access elements in the structure, use "->"

4. Structure nested structure

Pay attention to the usage of structure nested structure.

#include<iostream>
#include<string>
using namespace std;
struct stu
{
	string name;
	int age;
	int score;

};
struct teacher
{
	string name;
	int age;
	struct stu stu1;
};
int main()
{
	teacher t;
	t.name = "a";
	t.age = 50;
	t.stu1.age = 20;
	t.stu1.name = "b";
	t.stu1.score = 90;
	cout << "老师姓名:" << t.name << " 老师学生姓名:" << t.stu1.name << endl;
	system("pause");
	return 0;
}

5. Structure as function parameter

There are two ways to pass structure type data into parameters: value passing and address passing;

Value transfer: will not change the value of the structure in the main function;

Address transfer: will change the value of the structure in the main function.

#include <iostream>
using namespace std;
#include<string>
struct student
{
    string name;
    int age;
    int score;
};
void printstu(struct student stu)
{
    stu.age = 18;
    cout << "子函数1姓名:" << stu.name << " 子函数1年龄:" << stu.age << " 子函数1分数:" << stu.score << endl;
}
void printstu2(student *p)
{
    p->age = 18;
    cout << "子函数2姓名:" << p->name << " 子函数2年龄:" << p->age << " 子函数2分数:" << p->score << endl;
}
int main()
{
    student stu;
    stu.name = "hhhh";
    stu.age = 16;
    stu.score = 90;
    printstu(stu);
    cout << "主函数1姓名:" << stu.name << " 主函数1年龄:" << stu.age << " 主函数1分数:" << stu.score << endl;
    printstu2(&stu);
    cout << "主函数2姓名:" << stu.name << " 主函数2年龄:" << stu.age << " 主函数2分数:" << stu.score << endl;
    system("pause");
    return 0;
}

6, the use of const in the structure.

When passing a structure as a function parameter, if you use value transfer, the memory needs to copy all the values ​​in the structure and assign it to the function when passing it;

If the pointer is used to pass, the pointer only occupies four bytes (32 bits), and the required memory is greatly reduced. Modify the original value by mistake in the function, and change the pointer type parameter to const type.

#include <iostream>
using namespace std;
#include<string>
struct student
{
    string name;
    int age;
    int score;
};

void printstu2(const student *p)
{
    //p->age = 18;//错误操作,const修饰*,此时不能改变指针指向的值;
    cout << "子函数2姓名:" << p->name << " 子函数2年龄:" << p->age << " 子函数2分数:" << p->score << endl;
}
int main()
{
    student stu;
    stu.name = "hhhh";
    stu.age = 16;
    stu.score = 90;
   
    printstu2(&stu);
    cout << "主函数2姓名:" << stu.name << " 主函数2年龄:" << stu.age << " 主函数2分数:" << stu.score << endl;
    system("pause");
    return 0;
}

 

Guess you like

Origin blog.csdn.net/yyyllla/article/details/109299343