Today, we are going to learn about pointers in C language. Pointer can be said to be a heavy role in C language. We can understand and master pointer. In the future, when we write code, we feel like a fish in water and at ease. Next, let's have a look!!!
Contents of this chapter:
1. What is the pointer?
In computer science, a pointer is an object in a programming language. Using an address, its value directly points to a value stored in another place in the computer memory. Since the required variable unit can be found through the address, it can be said that the address points to the variable unit. Therefore, the address is visualized as a pointer. It means that the memory unit with its address can be found through it.
Then we can understand it this way:
Memory:
Pointer: a pointer is a variable that holds the address number of the memory unit. We can get the pointer through the following code:
#include<stdio.h> int main() { int a = 10;//We open up a space here int* p = &a;//Take the address of variable a with the & operator, and p is a pointer variable printf("%p\n", p); return 0; }
Therefore, the pointer is a variable used to store the address variable. The values stored in the pointer are treated as addresses.
The problem here is:
1. What is the address of a memory unit—— One byte, 8 binary bits.
2. How to address?
After careful calculation and trade-off, we find that it is more appropriate to give a corresponding address to a byte. For 32-bit machines, if there are 32 address lines, if each address line generates high level and low level during addressing, that is, 1 or 0 in the computer, then 32 address lines can generate 32-bit binary sequence. Accordingly, there will be 32 addresses to the power of 2. If each address identifies a byte, there will be (2 ^ 32 byte = = 2 ^ 32 / 1024 KB = = 2 ^ 32 / 1024 / 1024 MB = = 2 ^ 32 / 1024 / 1024 GB = = 4GB) 4G space for addressing.
Similarly, on 64 bit platforms, the address is 8 bytes.
2. Pointer and pointer type
As we all know, variables have different types, such as integer, floating point, etc. To be exact, pointers also have types
When there is such a code:
int num = 10; p = #
To save & num (the address of Num) to p, we know that p is a pointer variable. What is its type? We give the pointer variable the corresponding type:
char* pa = NULL; int* pb = NULL; short* pc = NULL; long* pd = NULL; float* pe = NULL; double* pf = NULL;
Here we can see that the pointer is defined as type +.
In fact, the pointer of char type is used to store variables of char type
Pointers of type int * are used to store variables of type int,
A pointer of type short * is used to hold a variable of type short
What is the meaning of pointer type?
2.1 pointer + integer
#include<stdio.h> int main() { int n = 10; char* pc = (char*)&n; //Here, a pointer of type int * is cast to a pointer of type char * int* pi = &n; printf("%p\n", &n); //004FFBB0 printf("%p\n", pc); //004FFBB0 printf("%p\n", pc + 1);//004FFBB1 printf("%p\n", pi); //004FFBB0 printf("%p\n", pi + 1);//004FFBB4 return 0; }
Through the above code, we find that the step of int pointer is 4 bytes, and the step of char pointer is 1 byte. We can get that the type of pointer determines how big the pointer moves forward or backward.
2.2 dereference of pointer
#include<stdio.h> int main() { int n = 0x11223344; char* pc = (char*)&n; int* pi = &n; *pc = 0;//Focus on observing memory changes during debugging *pi = 0;//Focus on observing memory changes during debugging return 0; }
Summary: the type of pointer determines how much permission you have to dereference the pointer and how many bytes you can operate. For example, the pointer of char * determines that the reference can only access one byte, and the pointer dereference of int * can only access four bytes.
3. Field pointer
Wild pointer: the wild pointer is that the position pointed by the pointer is uncertain. Random, incorrect and unrestricted are called incorrect.
3.1 cause of formation
1. Pointer not initialized
#include<stdio.h> int main() { int* p; //Local variable pointer is uninitialized and defaults to random value *p = 20; return 0; }
2. Pointer cross-border access
#include<stdio.h> int main() { int arr[10] = { 0 }; int* p = arr; for (i = 0; i <= 11; i++) { *(p++) = i; //When the pointer points beyond the array arr range, p is the wild pointer } return 0; }
3. The space pointed to by the pointer is released
I will explain this knowledge point when developing dynamic memory.
3.2 how to avoid wild pointer
1. Pointer initialization
2. Be careful that the pointer is out of range
3. The space pointed to by the pointer is set to NULL in time
4. Avoid returning the address of local variables
5. Check the validity of the pointer before use
4. Pointer operation
4.1. Pointer ± integer
#define N_VALUES 5 float values[N_VALUES]; float* vp; for (vp = &values[10]; vp < &values[N_VALUES];) { *vp++ = 0; }
2. Pointer pointer
int my_strlen(char* s)//Calculate string length { char* p = s; while (*p != '\0')//'\ 0' is the end of string flag p++; return p - s; }
3. Pointer relation operation
for (vp = &values[10]; vp > &values[N_VALUES];) { *--vp = 0; } //Code simplification: modify the code as follows for (vp = &values[10]; vp > &values[N_VALUES];vp--) { *vp = 0; }
In fact, the task can be successfully completed on most compilers, but we still need to avoid this, because the standard does not guarantee its feasibility.
The standard stipulates that the pointer to the array element is allowed to be compared with the pointer to the memory location after the last element of the array. However, it is not allowed to compare with a pointer to the memory location before the first element.
5. Pointers and arrays
What is the array name? Let's look at the following code:
#include<stdio.h> int main() { int arr[10] = { 1,2,3,4,5,6,7,8,9,10 }; printf("%p\n", arr); //00AFFAB0 printf("%p\n", &arr[0]); //00AFFAB0 return 0; }
We can see that the array name is the same as the address of the first element of the array. The conclusion is that the array name is actually the address of the first element of the array
6. Secondary pointer
Pointer variable is also a variable. If there is a variable, there is an address. Where is the address of pointer variable stored? The answer is a secondary pointer. I will explain the content of secondary pointer in the pointer of high-level C language. Please look forward to it.
7. Pointer array
Is a pointer array an array or a pointer? The answer is array!
int* arr[5];
arr is an array with five elements, each of which is an integer pointer.
Let's talk about the pointer first. For the initial pointer, you only need to be able to use it simply without further study. I will dig deeper into pointers with you in the high-order pointers section.