①Null pointer and wild pointer ②Const modified pointer ③Application of pointer and array ④Application of pointer and function ⑤Bubbling in ascending order (pointer)

Null pointer and wild pointer

1. Null pointer:

int * p = NULL;

[Pointing] : the space numbered 0

[Use] : initialized pointer variable

[Note] : null pointer memory is not accessible; 0-255 is the default memory space

2. Wild pointer:

int * p1 = (int*)0x1100;

Null pointers and wild pointers are not accessible

#include <iostream>
using namespace std;

int mian() {
    
    
	//空指针
	//指向 : 编号为0的空间
	//用途 :初始化的指针变量
	//注意 : 空指针的内存是不可以访问的;0-255都是系统默认的内存空间
	int* p = NULL;
	//cout << *p << endl;//错误
	//*p = 100;//错误

	野指针
	int* p1 = (int*)0x1100;
	//cout << *p1 << endl;//错误

	//空指针和野指针都是不能访问的
	return 0;
}

const modified pointer

[ Memory method ]:
Whoever const follows, who can't change
const is called a constant , * is called a pointer

1. Constant pointer

-> Pointing can be changed, value cannot be changed

const int* p1 = &a;
p1 = &b;
cout << *p1 << endl;//输出20

2. Pointer constant

-> Pointing cannot be changed, value can be changed

int* const p2 = &a;
*p2 = 100;
cout << *p2 << endl;//输出100

3. Neither the pointing nor the value can be changed

const int* const p = &a;

#include <iostream>
using namespace std;

int main()
{
    
    
	int a = 10;
	int b = 20;
	//常量指针->指向可改,值不可改
	const int* p1 = &a;
	p1 = &b;
	cout << *p1 << endl;//输出20
	//*p1 = 100; // const距离*最近,则取*的值不能改变

	//指针常量->指向不可改,值可改
	int* const p2 = &a;
	//p2 = &b; // const距离p2最近,则取p2的值不能改变
	*p2 = 100;
	cout << *p2 << endl;//输出100

	//都不能改
	const int* const p = &a;

	//记忆方法:const紧接着谁,谁就不能改变
	//const叫做常量,*叫做指针

	return 0;
}

Application of pointers and arrays

1、 *(p + i)

for (int i = 0; i < 10; i++)
{
	cout << *(p + i) << endl;
}

2. *p and p++

for (int i = 0; i < 10; i++)
{
	cout << *p << endl;
	p++;
}
#include <iostream>
using namespace std;

int main()
{
    
    
	//指针与数组
	int arr[10] = {
    
     1,2,3,4,5,6,7,8,9,10 };
	int* p = arr;
	cout << *p << endl;
	for (int i = 0; i < 10; i++)
	{
    
    
		cout << *(p + i) << endl;
	}
	for (int i = 0; i < 10; i++)
	{
    
    
		cout << *p << endl;
		p++;
	}
	return 0;
}

Application of pointers and functions

1. Value transfer

Can not change actual parameters through formal parameters

2. Address delivery

The actual parameters can be changed through the formal parameters

#include <iostream>
using namespace std;
//1.值传递函数
void swap01(int num1, int num2)
{
    
    
	int temp = num1;
	num1 = num2;
	num2 = temp;
}
//2.地址传递函数
void swap02(int *p1,int *p2)
{
    
    
	int temp = *p1;
	*p1 = *p2;
	*p2 = temp;
}
int main()
{
    
    
	//函数与指针
	int a = 10;
	int b = 20;
	//1.值传递
	swap01(a, b);
	cout << "a = " << a << "," << "b = " << b << endl;
	//2.地址传递
	swap02(&a, &b);
	cout << "a = " << a << "," << "b = " << b << endl;
	return 0;
}

Bubbling in ascending order (pointer)

Topic : Encapsulate a function and arrange the array in ascending order using bubble sorting

  • Step 1: Create an array
  • Step 2: Bubble function
  • Step 3: Pass the address of the array, arr is the first address of the array
  • Step 4: Output the array
#include <iostream>
using namespace std;
//封装一个函数,用冒泡排序法将数组升序排列
 
//第二步 :冒泡函数
void bubbleSort(int* arr, int len)
{
    
    
	for (int i = 0; i < len; 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;
			}
		}
	}

}
//输出函数
void printArray(int *arr, int len)
{
    
    
	for (int i = 0; i < len; i++)
	{
    
    
		cout << arr[i] << endl;
	}
}
int main()
{
    
    
	//第一步 :创建一个数组
	int arr[10] = {
    
     2,5,6,3,4,7,1,9,24,13 };
	int len = sizeof(arr) / sizeof(arr[0]);//数组长度
	//第三步 :传递数组的地址,arr就是数组的首地址
	bubbleSort(arr, len);
	//第四步 : 输出数组
	printArray(arr, len);
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_42198265/article/details/113801992