2021-01-24 Check in and learn C++ Day 4


One, pointer

The memory can be accessed indirectly through the pointer, and the number in the memory can be read or written

  • The memory number is recorded starting from 0, generally represented by hexadecimal numbers

  • You can use pointer variables to save addresses

grammar:数据类型 * 变量名;

Example

#include<iostream>
using namespace std;

int main()
{
    
    
	int a = 10;
	//指针定义
	int *p;
	//让指针记录变量a的地址
	p = &a;
	cout << "a的地址为:" << &a << endl;
	cout << "指针p为:" << p << endl;

	//可以通过解引用的方式来找到指针指向的内存
	//指针前加 * 代表解引用,找到指针指向的内存中的数据
	*p = 1000;
	cout << "a = " << a << endl;
	cout << "*p = " << *p << endl;




	system("pause");
	return 0;

}

Output result
Insert picture description here
Dereferencing is to return the corresponding object in the memory address

In a 32-bit operating system, a pointer occupies 4 bytes of space, regardless of the data type

In a 64-bit operating system, the pointer occupies a byte space size

Null pointer: The pointer variable points to the space numbered 0 in the memory, which is used to initialize the pointer variable

Note The memory pointed to by the null pointer is not accessible (the memory number between 0 and 255 is occupied by the system, so it cannot be accessed)

Wild pointer: Pointer variable points to illegal memory space

Null pointers and wild pointers are not the space we apply for, so do not visit

1, const modified pointer

  1. const modified pointer-constant pointer

  2. const modified constant-pointer constant

  3. const modifies both pointers and constants

Constant pointer: The pointer point can be changed, but the value pointed to by the pointer cannot be changed

const int * p = &a;
	int a = 10;
	int b = 10;
	int *p = &a;
	
	*p = 20;  //错误,指针指向的值不可以改
	p = &b;   //正确,指针指向可以改 

Pointer constant: The pointing of the pointer cannot be changed, the value pointed to by the pointer can be changed

int * const p = &a;
	int a = 10;
	int b = 10;
	int *p = &a;
	
	*p = 20;  //正确,指针指向的值可以改
	p = &b;   //错误,指针指向不可以改 

const modifies both pointers and constants: The pointer point and the value pointed to by the pointer cannot be changed

const int * const p = &a;

2. Pointers and arrays

Use pointers to access the contents of the array

Example

#include <iostream>
using namespace std;

int main()
{
    
    
	int arr[9] = {
    
     1,2,3,4,5,6,7,8,9 };
	int *p = arr;
	for(int i = 0; i < 9; i++)
	{
    
    
		cout << "利用指针输出数组中第" << i + 1<< "个数:" ;
		cout << *p << endl;
		p++;
	}


	system("pause");
	return 0;

}

Output result
Insert picture description here

3. Pointers and functions

Using pointers as function parameters, you can modify the value of the actual parameter (address transfer)

Example

#include <iostream>
using namespace std;

//地址传递,与值传递不同,地址传递可以改变主函数中实参的值
void swap(int *p1, int *p2)
{
    
    
	int temp = *p1;
	*p1 = *p2;
	*p2 = temp;
}
int main()
{
    
    
	int a = 10;
	int b = 20;
	swap(&a, &b);
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;


	system("pause");
	return 0;

}

Output result
Insert picture description here

3. Pointers, arrays, functions

Encapsulate a function and use bubble sorting to achieve ascending sorting of integer arrays

#include<iostream>
using namespace std;

//冒泡排序函数
void bubble(int *arr, int len)
{
    
    
	for (int i = 0; i < len - 1; i++)
	{
    
    
		for (int j = 0; j < len - i - 1; j++)
		{
    
    
			if (arr[j] > arr[j + 1])
			{
    
    
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}

int main()
{
    
    
	int arr[10] = {
    
     4,6,9,5,3,2,7,8,1,10 };
	int len = sizeof(arr) / sizeof(arr[0]);
	bubble(arr, len);
	for (int i = 0; i < 10; i++)
	{
    
    	
		cout << arr[i] << endl;	
	}

	system("pause");
	return 0;

}

Output result
Insert picture description here

Second, the structure

The structure belongs to the usercustomizeData types, allowing users to store different data types

1. Definition and use of structure

grammar:struct 结构体名 { 结构体成员列表 };

There are three ways to create variables through structures:

  • struct structure name variable name

  • struct structure name variable name = {member 1 value, member 2 value...}

  • By the way, create variables when defining the structure

Example

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

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

}s3;   //定义结构体时顺便创建变量

int main()
{
    
    
	// 1、struct 结构体名 变量名
	struct student s1;
	s1.name = "张三";
	s1.age = 17;
	s1.score = 90;
	cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;

	// 2、struct 结构体名 变量名 = { 成员1值,成员2值... }
	struct student s2 = {
    
     "李四",17,87 };
	cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 分数:" << s2.score << endl;

	//3、定义结构体时顺便创建变量
	s3.name = "王五";
	s3.age = 18;
	s3.score = 91;
	cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 分数:" << s3.score << endl;

	system("pause");
	return 0;

}

Output result
Insert picture description here

2. Structure array

Put the custom structure into the array for easy maintenance

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

Example

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

//1、定义结构体
struct student
{
    
    
	string name;
	int age;
	int score;

}; 

int main()
{
    
    
	//2、创建结构体数组
	struct student stu[2]=
	{
    
    
		{
    
    "张三", 18, 95},
		{
    
    "李四", 17, 90}
	};

	//3、给结构体数组中的元素赋值
	stu[1].name = "王五";

	//4、遍历结构体数组
	for (int i = 0; i < 2; i++)
	{
    
    
		cout << "姓名:" << stu[i].name
			 << " 年龄:" << stu[i].age 
			 << " 分数:" << stu[i].score << endl;
	}


	system("pause");
	return 0;

}

Output result
Insert picture description here

3. Structure pointer

Access the members of the structure through the pointer

Use operator -> to access structure attributes through structure pointers

==Access the properties in the structure through the structure pointer, you need to use '->' ==

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

//定义结构体
struct student
{
    
    
	string name;
	int age;
	int score;

}; 

int main()
{
    
    
	//创建结构体变量
	struct student s = {
    
     "张三", 18, 95 };

	//通过指针指向结构体变量
	struct student * p = &s;

	//通过指针访问结构体变量
	
		cout << "姓名:" << p->name
			 << " 年龄:" << p->age
			 << " 分数:" << p->score << endl;
	


	system("pause");
	return 0;

}

4. Structure nested structure

Example

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

//定义结构体
struct student
{
    
    
	string name;
	int age;
	int score;

};

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

};


int main()
{
    
    
	teacher t;
	t.id = 001;
	t.name = "老王";
	t.age = 50;
	t.stu.name = "小王";
	t.stu.age = 17;
	t.stu.score = 90;

	cout << "老师姓名:" << t.name
		<< " 老师id:" << t.id
		<< " 老师年龄:" << t.age<<endl
		<< "学生姓名:" << t.stu.name
		<< " 学生年龄:" << t.stu.age
		<< " 学生分数:" << t.stu.score << endl;
	


	system("pause");
	return 0;

}

5. Structure as function parameter

Pass the structure as a parameter like a function

Delivery method:

  • Pass by value

  • Address pass

Example

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

//定义结构体
struct student
{
    
    
	string name;
	int age;
	int score;

};

//打印学生信息
//1、值传递
void print01(struct student s)
{
    
    
	s.age = 100;
	cout << "print01学生姓名:" << s.name
		<< " 学生年龄:" << s.age
		<< " 学生分数:" << s.score << endl;

}
//2、地址传递
void print02(struct student *p)
{
    
    
	p->age = 200;
	cout << "print02学生姓名:" << p->name
		<< " 学生年龄:" << p->age
		<< " 学生分数:" << p->score << endl;
}

int main()
{
    
    
	struct student s;
	s.name = "张三";
	s.age = 18;
	s.score = 90;
	print01(s);
	cout << "main01学生姓名:" << s.name
		 << " 学生年龄:" << s.age
		 << " 学生分数:" << s.score << endl;
	
	
	print02(&s);
	cout << "main02学生姓名:" << s.name
		<< " 学生年龄:" << s.age
		<< " 学生分数:" << s.score << endl;


	system("pause");
	return 0;

}

Output result
Insert picture description here

6, the use of const in the structure

Use const to prevent misoperation
Change the formal parameters in the function to pointers, which can reduce the memory space and no longer need to make copies

Example

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

//定义结构体
struct student
{
    
    
	string name;
	int age;
	int score;

};

void print(const student *s)
{
    
    
	//加入const后,在函数中不可再修改结构体中数据属性
	cout << "学生姓名:" << s->name
		<< " 学生年龄:" << s->age
		<< " 学生分数:" << s->score << endl;
}
int main()
{
    
    
	struct student s = {
    
     "张三",20,90 };
	//通过函数来打印结构体信息
	print(&s);
	system("pause");
	return 0;

}

[Note] The learning course is-Dark Horse Program C++ Tutorial

Guess you like

Origin blog.csdn.net/qq_42616280/article/details/113068627