catalogue
2. Pointer cross-border access
3. The space pointed to by the pointer is released
Example 2: find the length of the string
4.3 pointer relation operation
preface
This blog is mainly about the initial pointer, the simple use of pointer, and the use of pointer in solving problems in daily use.
Tip: the following is the main content of this article
I What is the pointer?
Pointer: it is the number of memory unit, that is, pointer.
The pointer in spoken English is a pointer variable, which is used to store memory variables
Memory is divided into small memory units. The size of a basic memory unit is one byte
Address: memory unit number 0X00000000
Generation of memory unit number: 32-bit machine - 32 address line - Physical wire - power on - 1 / 0
00000000 00000000 00000000 00000000 -- 4 bytes
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000010
.... ... ... ...
11111111 11111111 11111111 11111111
2*10^32 bite -- 4GB
64 bit
int main() { int a = 10;// Four bytes int* pa = &a;//pa stores an address, so pa is a pointer variable printf("%d\n", sizeof(pa)); printf("%p", &a); return 0; }
The following picture is easy to understand:
give an example:
int main() { int a = 0x11223344; int* pa = &a; *pa = 0; //char* pc = &a; //*pc = 0; //int* --> 4 //char* --> 1 //double* --> 8 return 0; }
Printing of address:
II Pointer and pointer type
int main() { int a = 10; int* pa = &a;//Shaping -- 4 char* pc = &a;//Character -- 1 printf("%p\n", pa); printf("%p\n", pa+1); printf("%p\n", pc); printf("%p\n", pc+1); //The pointer type determines the distance the pointer takes one step forward or backward -- in bytes return 0; }
The pointer type determines the distance the pointer takes one step forward or backward -- in bytes
2.1} pointer dereference
2.2 pointer + - integer
Summary: the type of pointer determines how far the pointer moves forward or backward
give an example:
analysis:
int main() { int arr[10] = { 0 }; int* p = arr; int i = 0; for (i = 0; i < 10; i++) { //Initializing *(p+i) = i+1; } //Printing int* m = &arr[0]; for (i = 0; i < 10; i++) { printf("%d ", *m); m++; } printf("\n"); //Print backwards 9 8 7 int* q = &arr[9]; for (i = 0; i < 10; i++) { printf("%d ", *q); q--; } return 0; }
Demo analysis:
III Field pointer
Concept: a wild pointer means that the position pointed to by the pointer is unknown (random, incorrect, and without explicit restrictions)
3.1 cause of formation
1. Pointer not initialized
int main() { int* p;//Local variable pointer is uninitialized and defaults to random value *p = 20; return 0; }
2. Pointer cross-border access
int arr[10] = { 0 }; int* p = arr; for (int i = 0; i <= 10; i++) { *p = i; p++; }
3. The space pointed to by the pointer is released
//The space pointed to by the pointer is released int* test() { int a = 100; return &a; } int main() { int* p = test(); printf("%d", *p); return 0; } int main() { int a = 10; int* pa = &a;// Know who to deposit it with int* p = NULL;//Initialize to null pointer if (p != NULL) { } return 0; }
analysis:
3.2 how to avoid wild pointer
IV Pointer operation
4.1 pointer + - integer
Initialize and assign print int main() { int arr[10] = { 0 }; int* p = arr; int i = 0; int sz = sizeof(arr) / sizeof(arr[0]); for (i = 0; i < sz; i++) { *(p + i) = i+1; } //Print for (i = 0; i < sz; i++) { printf("%d ", *(p + i)); } return 0; }
4.2 pointer - pointer
int main() { /*int a[10] = { 0 }; printf("%d\n", &a[9] - &a[0]); printf("%d\n", &a[0] - &a[9]);*/ int a = 10; char c = 'w'; return 0; }
analysis:
Example 2: find the length of the string
Previously, we used the strlen function to find the length of the string
//Function strlen for string length #include <string.h> int My_strlen(char* s) { int count = 0; while(*s != '\0') { count++; s++; } return count; } int main() { char arr[] = "abc"; int len =My_strlen(arr); printf("%d\n", len); return 0; }
Similarly, we can also use the pointer minus pointer method
int My_strlen(char* s) { char* start = s; while (*s != '\0') { s++; } return s - start; } int main() { char arr[] = "abc"; int len =My_strlen(arr); printf("%d\n", len); return 0; }
analysis:
4.3 pointer relation operation
int main() { float a[5]; float* p; for (p = &a[5]; p >= &a[0];) { *--p = 0; } return 0; }
improvement:
for (p = &a[4]; p >= &a[0]; p--) { *p = 0; }
V Pointers and arrays
Pointer -- address array -- A set of data of the same type int main() { int arr[10] = { 1,2,3,4,5,6,7,8,9,10 }; //arr first element address int* p = arr; int i = 0; for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) { printf("%p == %p \n", p + i,&arr[i]); } return 0; }
analysis:
int main() { int arr[] = { 1,2,3,4,5,6,7,8,9,0 }; int* p = arr; //Pointer to the address of the first element of the array int sz = sizeof(arr) / sizeof(arr[0]); int i = 0; for (i = 0; i < sz; i++) { printf("&arr[%d] = %p <====> p+%d = %p\n", i, &arr[i], i, p + i); } return 0; }
So p+i actually calculates the address of array arr subscript i
Vi Secondary pointer
int main() { int a = 10; int* pa = &a; int** ppa = &pa; //ppa is a secondary pointer **ppa = 20; printf("%d\n",a); return 0; }
VII Pointer array
int main() { int arr[10];//Shaping array char ch[5];//Character array //Pointer array --- an array of pointers int a = 10; int b = 20; int c = 30; int* arr2[5] = {&a,&b,&c};//An array of integer pointers int i = 0; for (i = 0; i < 3; i++) { printf("%d ", *(arr2[i])); } return 0; }
summary
This article roughly summarizes the simple use of the pointer in daily learning. If it is helpful to you after reading, I hope you will like Collection + attention. Thank you for your support. If there are mistakes in the article, please correct them in time.