Deep understanding of pointers
preface
First, let's understand the concept of the following pointers:
- A pointer is a variable used to store an address, which identifies a piece of memory space.
- The size of the pointer is fixed at 4 or 8 bytes (32 or 64 bits).
- Pointers are typed, which determines the step size of pointer operation and the operation permission of dereference.
1, Character pointer
Character pointers, as the name suggests, are pointers to character types.
For example:
int main() { char ch = 'w'; char *pc = &ch; *pc = 'w'; return 0; }
The character pointer stores the address of a character.
There is another way to use it:
int main() { char* pstr = "hello";//Did you put a string into the pstr pointer variable? printf("%s\n", pstr); return 0; }
How to understand char* pstr = "hello"; Did you put a string into a pointer variable?
In fact, it's not. It just puts the address of the first character of the string into pstr. At the same time, a read-only string declared by this code cannot be modified. Add const modification.
Let's take another example:
int main() { char str1[] = "hello bit."; char str2[] = "hello bit."; const char* str3 = "hello bit."; const char* str4 = "hello bit."; if (str1 == str2) printf("str1 and str2 are same\n"); else printf("str1 and str2 are not same\n"); if (str3 == str4) printf("str3 and str4 are same\n"); else printf("str3 and str4 are not same\n"); return 0; }
What is the result of this code? Are their addresses the same?
Please see the following print results:
The addresses of str3 and str4 are the same because they point to the same constant string. C/C + + will store constant strings in a separate memory area. When multiple constant pointers point to the same string, they actually point to the address of the same block of memory.
However, initializing different arrays with the same constant string will open up different memory blocks, so str1 and str2 are different, and str3 and str4 are the same.
2, Pointer array
Pointer array, as the name suggests, is an array of pointers. For example:
- int* arr1[10]; // Integer pointer array
- char* arr2[10]; // First level character pointer array
- char** arr3[10]; // Secondary character pointer array
3, Array pointer
1. Definition of array pointer
First, the array pointer, which is a pointer. So the element type it points to is an array.
int *p1[10];
int (*p2)[10];
Which of the above two are array pointers?
Let's analyze. First, p1 is combined with []. Because * has a lower priority, it is an array and the element type is an integer pointer. The second p2 starts with a pointer to an integer array with ten elements. So the second is array pointers.
2. Array name and & array name
We know that the array name represents the address of the first element, & what about the array name?
Let's start with a code:
int main() { int arr[10] = {0}; printf("%p\n", arr); printf("%p\n", &arr); return 0; }
The result is:
It can be found that both printed addresses are the same. But are they really the same?
Let's look at the code:
int main() { int arr[10] = { 0 }; printf("arr = %p\n", arr); printf("&arr= %p\n", &arr); printf("arr+1 = %p\n", arr + 1); printf("&arr+1= %p\n", &arr + 1); return 0; }
The results are as follows:
Although the addresses they point to are the same, the pointer operation is indeed different, because we know that the pointer operation is determined according to the type pointed to by the pointer. Since the operation results are different, their types must be different.
In fact, & arr represents the address of the entire array. Although the address starts from the first element address, it is not the first element address. For the array address + 1, the size of an array will be skipped.
4, Function pointer
A function pointer is a pointer to a function:
int f(int); int (*pf)(int) = &f;`Insert the code slice here`
In the above two statements, the first one declares a function with parameter of type int and return value of type int. The second line defines and initializes a function pointer, and the function type pointed to is the function type of the first line.
1. Function pointer array
Function pointer array, first of all, is an array, and array elements are function pointers. As follows:
int f(int); //function int (*pf)(int) = &f; //Function pointer int (*pfarr[5]) (int); //Function pointer array
Function pointers are used to transfer tables. See the following code:
int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } int mul(int a, int b) { return a * b; } int div(int a, int b) { return a / b; } void menu() { printf("******************************\n"); printf("****** 1.add 2.sub ******\n"); printf("****** 3.mul 4.div ******\n"); printf("********** 0.exit **********\n"); printf("******************************\n"); } int main() { int input; int (*pfarr[5])(int, int) = { 0, add, sub, mul, div }; do { menu(); printf("Please select calculation method:"); scanf("%d", &input); if (0 == input) { printf("Exit program!\n"); } else if (input > 0 && input < 5) { int x; int y; printf("Please enter operand:"); scanf("%d %d", &x, &y); int temp = pfarr[input](x, y); printf("%d\n", temp); } else { printf("Input error, please re-enter!\n"); } } while (input); return 0; }
Call the function through the transfer table to realize different operations.
2. Callback function
A callback function is a function called through a function pointer. If you pass a function pointer as a parameter to another function, when the pointer is used to call the function it points to, we say it is a callback function. The callback function is not called directly by the implementer of the function, but by another party when a specific event or condition occurs, which is used to respond to the event or condition.
First, let's look at a qsort function:
int cmp_int(const void* e1, const void* e2) { if (*(int*)e1 < *(int*)e2) { return -1; } else if (*(int*)e1 > *(int*)e2) { return 1; } else { return 0; } } int main() { int arr[10] = { 8,6,2,9,7,3,5,1,4,0 }; int len = sizeof(arr) / sizeof(arr[0]); qsort(arr, len, sizeof(int), cmp_int); int i; for (i = 0; i < len; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; }
The fourth parameter of qsort is a function pointer, which is used to call this function in the qsort function.
summary
- Sizeof (array name), where the array name represents the entire array, and the size of the entire array is calculated.
- &Array name, where the array name represents the whole array, and the address of the whole array is taken out.
- In addition, all array names represent the address of the first element.
- You can use function pointers to implement callback functions.
- Transfer tables also use function pointers, but it should be noted that these functions must have the same function prototype.