Wiki
Pointer
Existing English translations were reused. 1 current source blocks remain in Simplified Chinese, without triggering paid translation.
1.Pointer
1.1.Basic concepts of pointers
Role of pointers: Memory can be accessed indirectly through pointers
-
Memory addresses are numbered starting from 0 and are generally represented in hexadecimal.
-
Pointer variables can be used to store addresses.
1.2.Definition and use of pointer variables
Pointer variable definition syntax: 数据类型 * 变量名;
Example:
int main() {
//1、指针的定义
int a = 10; //定义整型变量a
//指针定义语法: 数据类型 * 变量名 ;
int * p;
//指针变量赋值
p = &a; //指针指向变量a的地址
cout << &a << endl; //打印数据a的地址
cout << p << endl; //打印指针变量p
//2、指针的使用
//通过*操作指针变量指向的内存
cout << "*p = " << *p << endl;
return 0;
}
Running result (the address may differ on each run; denoted below by <地址>):
<地址>
<地址>
*p = 10
The difference between pointer variables and ordinary variables
- An ordinary variable stores data, while a pointer variable stores an address.
- A pointer variable can use the "*" operator to access the memory space that the pointer variable points to; this process is called dereferencing.
Summary 1: We can obtain the address of a variable using the & symbol
Summary 2: Pointers can be used to record addresses.
Summary 3: Dereferencing a pointer variable allows manipulation of the memory pointed to by the pointer
1.3.Memory space occupied by a pointer
Question: A pointer is also a data type, so how much memory space does this data type occupy?
Example:
int main() {
int a = 10;
int * p;
p = &a; //指针指向数据a的地址
cout << *p << endl; //* 解引用
cout << sizeof(p) << endl;
cout << sizeof(char *) << endl;
cout << sizeof(float *) << endl;
cout << sizeof(double *) << endl;
return 0;
}
Run result (type size or type name may vary depending on compiler and platform):
10
8
8
8
8
Summary: All pointer types are 4 bytes under a 32-bit operating system.
1.4.Null pointers and wild pointers
Null pointer : a pointer variable points to the memory space with address 0
**Purpose:** Initialize a pointer variable
**Note:** The memory pointed to by a null pointer cannot be accessed.
Example 1: Null pointer
int main() {
//指针变量p指向内存地址编号为0的空间
int * p = NULL;
//访问空指针报错
//内存编号0 ~255为系统占用内存,不允许用户访问
cout << *p << endl;
return 0;
}
Run result: The program triggered undefined behavior; this run received SIGSEGV and then terminated abnormally, with no reliable normal output.
Wild pointer: A pointer variable points to an invalid memory space
Example 2: Wild pointer
int main() {
//指针变量p指向内存地址编号为0x1100的空间
int * p = (int *)0x1100;
//访问野指针报错
cout << *p << endl;
return 0;
}
Run result: The program triggered undefined behavior; this run received SIGSEGV and then terminated abnormally, with no reliable normal output.
Summary: Neither null pointers nor wild pointers refer to space we have allocated, so do not access them.
1.5.const-qualified pointer
There are three cases of const modifying pointers.
- const modifying pointer --- pointer to constant
- const modifying constant --- constant pointer
- const modifying both pointer and constant
Example:
int main() {
int a = 10;
int b = 10;
//const修饰的是指针,指针指向可以改,指针指向的值不可以更改
const int * p1 = &a;
p1 = &b; //正确
//*p1 = 100; 报错
//const修饰的是常量,指针指向不可以改,指针指向的值可以更改
int * const p2 = &a;
//p2 = &b; //错误
*p2 = 100; //正确
//const既修饰指针又修饰常量
const int * const p3 = &a;
//p3 = &b; //错误
//*p3 = 100; //错误
return 0;
}
Run result: The program ended normally, with no output in the terminal.
Tip: Look at whether a pointer or a constant immediately follows the right side of `const`. If it is a pointer, it is a pointer to const; if it is a constant, it is a const pointer.
1.6.Pointers and arrays
**Purpose:** Use pointers to access elements in an array
Example:
int main() {
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int * p = arr; //指向数组的指针
cout << "第一个元素: " << arr[0] << endl;
cout << "指针访问第一个元素: " << *p << endl;
for (int i = 0; i < 10; i++)
{
//利用指针遍历数组
cout << *p << endl;
p++;
}
return 0;
}
Operation result:
第一个元素: 1
指针访问第一个元素: 1
1
2
3
4
5
6
7
8
9
10
1.7.Pointers and Functions
**Purpose:** Using pointers as function parameters allows modifying the values of the actual arguments.
Example:
//值传递
void swap1(int a ,int b)
{
int temp = a;
a = b;
b = temp;
}
//地址传递
void swap2(int * p1, int *p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int main() {
int a = 10;
int b = 20;
swap1(a, b); // 值传递不会改变实参
swap2(&a, &b); //地址传递会改变实参
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
Operation result:
a = 20
b = 10
Summary: If you do not want to modify the actual argument, use pass-by-value; if you want to modify the actual argument, use pass-by-address
1.8.Pointers, Arrays, Functions
**Case Description:** Encapsulate a function that uses bubble sort to sort an integer array in ascending order.
例如数组:int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };
Example:
//冒泡排序函数
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])
{
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] = { 4,3,6,9,1,2,10,8,7,5 };
int len = sizeof(arr) / sizeof(int);
bubbleSort(arr, len);
printArray(arr, len);
return 0;
}
Run result (type size or type name may vary depending on compiler and platform):
1
2
3
4
5
6
7
8
9
10
Summary: When an array name is passed to a function as an argument, it decays to a pointer to its first element