I like to specialize in the knowledge I love and the joy of completing the project
come on. When your strength can't meet your goals, calm down and learn!
Pointer
Basic concept of pointer
Function of pointer: memory can be accessed indirectly through pointer
- The memory number starts from 0 and is generally represented by hexadecimal digits
- Pointer variables can be used to save addresses
Definition and use of pointer variables
- Pointer variable definition syntax: data type * variable name;
Example:
int main() { //1. Definition of pointer int a = 10; //Define integer variable a //Pointer definition syntax: data type * variable name; int * p; //Pointer variable assignment p = &a; //The pointer points to the address of variable a cout << &a << endl; //Address of print data a cout << p << endl; //Print pointer variable p //2. Use of pointers //Memory pointed to by * operation pointer variable cout << "*p = " << *p << endl; system("pause"); return 0; }
Difference between pointer variable and ordinary variable
- Ordinary variables store data, and pointer variables store addresses
- Pointer variables can operate the memory space pointed to by pointer variables through the "*" operator. This process is called dereference
Summary:
- 1. We can get the address of the variable through the & symbol
- 2. The address can be recorded with a pointer
- 3. Dereference the pointer variable to operate the memory pointed to by the pointer
Memory space occupied by pointer
- Pointer is also a data type. How much memory does this data type occupy?
Example:
int main() { int a = 10; int * p; p = &a; //The pointer points to the address of data a cout << *p << endl; //*Dereference cout << sizeof(p) << endl; cout << sizeof(char *) << endl; cout << sizeof(float *) << endl; cout << sizeof(double *) << endl; system("pause"); return 0; }
Summary: all pointer types are 4 bytes in 32-bit operating system and 8 bytes in 64 bit operating system
- x86_32 abbreviated as x86
- x86_64 --> x64
Null pointer and wild pointer
-
Null pointer: pointer variable points to the space numbered 0 in memory
-
Purpose: initialize pointer variables
Note: the memory pointed to by the null pointer is inaccessible
- Memory numbers 0 ~ 255 are the memory occupied by the system and are not allowed to be accessed by users
Example 1: null pointer
int main() { //The pointer variable p points to the space with memory address number 0 int * p = NULL; //Error accessing null pointer //Memory numbers 0 ~ 255 are the memory occupied by the system and are not allowed to be accessed by users cout << *p << endl; system("pause"); return 0; }
- Wild pointer: pointer variable points to illegal memory space
Example 2: field pointer
int main() { //The pointer variable p points to the space with the memory address number 0x1100 int * p = (int *)0x1100; //Error in accessing field pointer cout << *p << endl; system("pause"); return 0; }
Conclusion: null pointer and wild pointer are not the space we apply for, so do not access.
Null pointers expand knowledge:
-
void * this is not a null pointer, it is a pointer without an exact type This pointer points to a piece of memory without telling the program how to interpret it Therefore, this type of pointer cannot directly fetch content You must convert to another type of pointer before you can interpret the content
-
And '\ 0', which is not what the NULL pointer refers to. '\ 0 'means the end of a string, not NULL
-
The real NULL pointer means that the pointer does not point to a meaningful memory, such as: char* k; This k is called a NULL pointer We didn't let it point anywhere Or char* k = NULL; Here, K is also called NULL pointer because it points to NULL, that is, 0. Note that it is an integer 0, not '\ 0'
-
We can't fetch the contents of a null pointer A null pointer can be fetched only after it actually points to a meaningful piece of memory That is to say, k = "hello world!"; At this point K is not a null pointer
const modifier pointer
const modifier pointer has three cases
- const modifier pointer - constant pointer
- const modifier constant - pointer constant
- const modifies both pointers and constants
Example:
int main() { int a = 10; int b = 10; //const modifies the pointer. The pointer can be changed, and the value pointed to by the pointer cannot be changed const int * p1 = &a; p1 = &b; //correct //*p1 = 100; report errors //const is a constant. The pointer cannot be changed, and the value pointed to by the pointer can be changed int * const p2 = &a; //p2 = &b; // error *p2 = 100; //correct //const modifies both pointers and constants const int * const p3 = &a; //p3 = &b; // error //*p3 = 100; // error system("pause"); return 0; }
Tip: look at whether the pointer or constant is immediately followed on the right side of const. Whether the pointer is a constant pointer or a constant is a pointer constant
Pointers and arrays
- Function: use pointer to access elements in array
Example:
int main() { int arr[] = { 1,2,3,4,5,6,7,8,9,10 }; int * p = arr; //Pointer to array cout << "First element: " << arr[0] << endl; cout << "Pointer to access the first element: " << *p << endl; for (int i = 0; i < 10; i++) { //Traversing arrays with pointers cout << *p << endl; p++; } system("pause"); return 0; }
Pointers and functions
- Function: use pointers as function parameters to modify the value of arguments
Example:
//pass by value void swap1(int a ,int b) { int temp = a; a = b; b = temp; } //Address delivery void swap2(int * p1, int *p2) { int temp = *p1; *p1 = *p2; *p2 = temp; } int main() { int a = 10; int b = 20; swap1(a, b); // Value passing does not change the argument swap2(&a, &b); //Address passing changes the arguments cout << "a = " << a << endl; cout << "b = " << b << endl; system("pause"); return 0; }
Summary: if you don't want to modify the argument, pass it by value. If you want to modify the argument, pass it by address
Summary case
- Using the knowledge of pointer, array and function, encapsulate a function, and use bubble sorting to realize the ascending sorting of integer array
For example, array: int arr [10] = {4,3,6,9,1,2,10,8,7,5};
//Bubble sort function void bubbleSort(int * arr, int len) //int * arr can also be written as 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; } } } } //Print array function 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); system("pause"); return 0; }