C++ stage 01 notes 07 [Pointers (basic concepts, variable definition and use, memory space, null pointers and wild pointers, const modified pointers, pointers and arrays, pointers and functions)]

C++| Ingenious work from 0 to 1 introductory programming [video + courseware + notes + source code]

table of Contents

7 pointer

7.1 Basic concepts of pointers

7.2 Definition and use of pointer variables

Example

7.3 Memory space occupied by pointers

Example

7.4 Null pointer and wild pointer

Example 1: Null pointer

Example 2: Wild pointer

to sum up

7.5 const modified pointer

1, const modified pointer-constant pointer

2. const modified constant-pointer constant

3. const modifies both pointers and constants

Example

7.6 Pointers and arrays

Example

7.7 Pointers and functions

Example

7.8 Pointers, arrays, functions

Example


7 pointer

7.1 Basic concepts of pointers

The role of pointers: memory can be accessed indirectly through pointers .

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

  • You can use pointer variables to save addresses.

The memory has an address number, starting from 0 to record, generally using hexadecimal numbers to represent the number, you can use a pointer to record the address number.

int a = 10; // 4 bytes of space

Every time a variable is created, the address number must be recorded. It is not convenient to use this data, so this variable is available.

Know the address number, you can also get the data. The pointer is an address, record the address number!

7.2 Definition and use of pointer variables

Pointer variable definition syntax:数据类型 * 变量名;

Example

The difference between pointer variables and ordinary variables:

  • Ordinary variables store data, and pointer variables store addresses.

  • Pointer variables can use the "*" operator to manipulate the memory space pointed to by pointer variables. This process is called dereferencing .

 The memory can be found by p, and the memory can be modified by *p.

The role of the pointer (purpose): it can indirectly access the memory, and read and write operations (modification, access) to it!

  

#include <iostream>
using namespace std;

int main()
{
	//1、指针的定义
	int a = 10; //定义整型变量a

	//指针定义的语法:数据类型 * 指针变量名 ;
	int *p; // point代表指针

	//指针变量赋值 让指针记录变量a的地址
	p = &a;	//指针p指向(等于)变量a的地址【取地址符号& 取变量a的地址】建立变量与指针之间的关系
	cout << "a的地址为:" << &a << endl; //打印数据a的地址:0x61fe14
	// cout << "a的地址为:" << (int)&a << endl; //十六进制转整型
	cout << "指针p为:" << p << endl; //打印指针变量p:0x61fe14【指针就是一个地址,记录地址编号!】

	//2、指针的使用
	//可以通过“解引用”的方式来找到指针指向的内存
	//指针前加一个* 代表 解引用,找到指针指向的内存中的数据
	//通过*操作指针变量指向的内存
	cout << "*p = " << *p << endl; // 10

	*p = 1000; //解引用 通过指针间接地找到了a的内存 通过解引用拿到p指向的内存中的数据(进行 修改、读取)
	cout << "a = " << a << endl;
	cout << "*p = " << *p << endl;

	system("pause");
	return 0;
}

Summary 1: We can get the address of the variable through the & symbol.

Summary 2: Use pointers to record addresses.

Summary 3: Dereference the pointer variable, you can manipulate the memory pointed to by the pointer.

7.3 Memory space occupied by pointers

Question: Pointers are also a data type, so how much memory space does this data type occupy?

Summary: All pointer types are 4 bytes under a 32-bit operating system, and 8 bytes under a 64-bit operating system.

Example

 

#include <iostream>
using namespace std;

int main() //指针所占内存空间
{
	int a = 10;

	// int *p;
	// p = &a; //指针指向数据a的数据地址
	int *p = &a; //建立关系

	//在32位操作系统下,指针是占4个字节空间大小(不管是什么数据类型)
	//在64位操作系统下,指针是占4个字节空间大小(不管是什么数据类型)
	cout << *p << endl;										   //10 * 解引用
	cout << "sizeof(p) = " << sizeof(p) << endl;			   //8 查看变量(数据类型)占用的内存空间
	cout << "sizeof(int *) = " << sizeof(int *) << endl;	   //8
	cout << "sizeof(float *) = " << sizeof(float *) << endl;   //8
	cout << "sizeof(double *) = " << sizeof(double *) << endl; //8
	cout << "sizeof(char *) = " << sizeof(char *) << endl;	   //8
	system("pause");
	return 0;
}

7.4 Null pointer and wild pointer

Null pointer : The pointer variable points to the space numbered 0 in the memory. Memory sticks have their own numbers, which increase from 0. Pointing to the pointer numbered 0 is called a "null pointer"!

Purpose: to initialize pointer variables. As soon as the pointer does not know where to point, it points to the space numbered 0 in the memory.

Note: The memory pointed to by the null pointer is not accessible. No right to access the memory pointed to by the null pointer. The memory between 0 and 255 is occupied by the system. Once accessed, an error will occur!

Example 1: Null pointer

#include <iostream>
using namespace std;

int main() //空指针
{
	//1、空指针用于给指针变量进行初始化
	// int * p;//指针指向哪?未知!所以,一般会让指针指向NULL(空)
	int *p = NULL; //指针变量p指向内存地址编号为0的空间

	//2、空指针是不可以进行访问的
	//0~255之间的内存编号是系统占用的,因此不可以访问
	*p = 100; //直接引用 操作内存
	//访问空指针报错
	//内存编号0 ~255为系统占用内存,不允许用户访问
	cout << *p << endl;

	//system("pause");
	return 0;
}

Example 2: Wild pointer

Wild pointer : The pointer variable points to an illegal memory space. Illegal memory space: it is not the memory space requested by the user.

#include <iostream>
using namespace std;

int main() //野指针 在程序中,尽量避免出现野指针
{
	// int *p = NULL;
	//指针变量p指向内存地址编号为0x1100的空间【0x1100:十六进制数字】
	int *p = (int *)0x1100; //变为地址:(int *)强转为指针类型
	//0x1100随便在内存中指向了这样一个编号,这个编号中的数 无权利操作!没有申请,无权利操作!
	//举例:花钱买房间A(int a = 10; int *p = &a;),没有权利去房间B(房间B->野指针)

	cout << *p << endl; //访问野指针报错

	system("pause");
	return 0;
}

to sum up

Summary: Null pointers and wild pointers are not the space we applied for, so do not visit.

7.5 const modified pointer

There are 3 cases of const modified pointer:

  1. const modified pointer - constant pointer         const int * p1 = &a;

  2. const modified constant - pointer constant         int *const p2 = &a;

  3. const modifies both pointers and constants

1, const modified pointer-constant pointer

Red box: restricted and can not be modified; black line: can be modified.

2. const modified constant-pointer constant

Red line: restricted and cannot be modified; black box: can be modified.

3. const modifies both pointers and constants

Example

 

#include <iostream>
using namespace std;

int main()
{
	int a = 10;
	int b = 10;
	//int *p0 = &a; //普通写法

	//1、常量指针(记法:const在前 先常量 后指针)
	//const修饰的是指针,指针的指向可以更改,指针指向的值不可以更改(可以理解为const 修饰的是解引用 int*,所以指针指向的值不可以更改)
	const int *p1 = &a;
	p1 = &b; //正确
	// *p1 = 20; //错误

	//2、指针常量(记法:int* 在前 先指针 后常量)
	//const修饰的是常量,指针的指向不可以更改,指针指向的值可以更改(可以理解为const修饰的是指针本身,所以指针指向的值不可以修改)
	int *const p2 = &a;
	*p2 = 100; //正确
	//p2 = &b; //错误,指针常量 指针的指向不可以更改

	//3、const既修饰指针又修饰常量
	const int *const p3 = &a;
	//指针的指向和指针指向的值都不可以改
	//*p3 = 100; //错误
	//p3 = &b; //错误

	system("pause");
	return 0;
}

Tips: Look at whether the right side of const is a pointer or a constant , a pointer is a constant pointer , and a constant is a pointer constant .

7.6 Pointers and arrays

Role: Use pointers to access elements in the array. Array: In a continuous space, data elements of the same type are stored.

Example

  

#include <iostream>
using namespace std;

int main()
{
	//指针和数组
	//让(利用)指针也能访问数组中的每一个元素
	int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	cout << "第一个元素为:" << arr[0] << endl;

	int *p = arr; //指向数组的指针 数组是整型的,所以创建整型指针,指向数组的地址 arr数组名就是数组的首地址
	//指针指向数组首地址,对指针进行解引用的操作,就可以解出数组中的第一个元素
	cout << "利用指针访问第一个元素:" << *p << endl; //*p解引用

	p++; //指向数组中的第二个元素,让指针向后偏移(移动)4个字节(整型指针)
	cout << "利用指针访问第二个元素:" << *p << endl; //*p解引用

	cout << "利用指针遍历数组:" << endl;
	int *p2 = arr;
	for (int i = 0; i < 10; i++) //利用指针遍历数组
	{
		// cout << arr[i] << endl;
		cout << *p2 << endl;
		p2++;
	}

	system("pause");
	return 0;
}

7.7 Pointers and functions

Role: Use pointers as function parameters to modify the value of actual parameters.

Example

 

The pointer holds the address. By passing the address, the actual parameter data can be changed indirectly.

#include <iostream>
using namespace std;

//1、值传递:实现两个数字进行交换
void swap01(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;
	cout << "swap01中 a = " << a << endl;
	cout << "swap01中 b = " << b << endl;
}

//2、地址传递
void swap02(int *p1, int *p2)
{
	int temp = *p1; //解出内存 解引用
	*p1 = *p2;
	*p2 = temp;
	cout << "swap02中 *p1 = " << *p1 << endl;
	cout << "swap02中 *p2 = " << *p2 << endl;
}

int main() //指针和函数
{
	int a = 10;
	int b = 20;

	//1、值传递不会改变实参
	swap01(a, b);
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;

	//2、地址传递会改变实参
	//将a、b变量地址传入函数体中,用指针接受地址【如果是地址传递,可以修饰实参】
	swap02(&a, &b);
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;

	system("pause");
	return 0;
}

Summary: If you don't want to modify the actual parameter, just pass it by value, if you want to modify the actual parameter, just pass it by address.

7.8 Pointers, arrays, functions

Case description: Encapsulate a function and use bubble sorting to achieve ascending sorting of integer arrays.

For example, array: int arr[10] = {4,3,6,9,1,2,10,8,7,5 };

Example

#include <iostream>
using namespace std;

//冒泡排序函数【参数1:数组首地址;参数2:数组长度】
void bubbleSort(int *arr, int len) //int * arr 也可以写为int arr[]
{
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - 1 - i; j++)
		{
			if (arr[j] > arr[j + 1]) //如果j > j + 1的值,交换数字
			{
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}

//打印数组函数
void printArray(int arr[], int len) //void printArray(int *arr, int len)
{
	for (int i = 0; i < len; i++)
	{
		// cout << arr[i] << endl;
		cout << arr[i] << "、";
	}
}

int main()
{
	//1、创建一个数组
	int arr[10] = {4, 3, 6, 9, 1, 2, 10, 8, 7, 5};
	//数组长度
	int len = sizeof(arr) / sizeof(int);

	//2、创建一个函数实现冒泡排序
	bubbleSort(arr, len); //传递数组地址arr:数组名就是数组的首地址

	//3、打印排序后的数组
	printArray(arr, len); //传递数组地址arr

	system("pause");

	return 0;
}

Summary: When the array name is passed into a function as a parameter, it is degenerated into a pointer to the first element.

Guess you like

Origin blog.csdn.net/weixin_44949135/article/details/115187829