Pointer
What is a pointer?
In computer science, a Pointer is an object in a programming language whose value points directly to another place in the computer memory by using an address. Since the desired variable unit can be found by address, it can be said that the address points to the variable unit. Therefore, visualizing an address is called a "pointer". This means that it can find the memory unit to which it is addressed.
A better understanding is that the pointer is the number of the memory address.
int main() { int a = 10;//Open up a space in memory int *p = &a;//Here we take out the address of variable a and use the &operator. return 0; } //Place the address of a in the P variable, where p is a pointer variable.
Summary: Pointers are variables that store addresses
Pointer size: 4 bytes in 32-bit OS and 8 bytes in 64-bit OS
Pointer and pointer type
To save &a (the address of a) to p, we know that P is a pointer variable, so we define the type of pointer based on the type of A. Common pointer types are:
char *pc = NULL; int *pi = NULL; short *ps = NULL; long *pl = NULL; float *pf = NULL; double *pd = NULL;
There are also some uncommon pointer types:
Pointer Array int* arr1[10]; //Array of reshaping pointers char *arr2[4]; //Array of first-level character pointers char **arr3[5];//Array of secondary character pointers Array Pointer int (*p2)[10];//Pointer to save array Function Pointer void (*pfun1)(); Function Pointer Array int (*parr1[10]])();
As you can see here, the pointer is defined as type + *. In fact, a pointer of type char* is used to store the address of a variable of type char. A pointer of type short* is used to store the address of a variable of type short. Pointers of type int* are used to store addresses for variables of type int.
Addition or subtraction of pointer
The pointer can hold addresses, so the addition or subtraction of the pointer is also related to addresses.
int main() { int n = 10; char *pc = (char*)&n; int *pi = &n; printf("%p\n", &n); printf("%p\n", pc); printf("%p\n", pc+1); printf("%p\n", pi); printf("%p\n", pi+1); return 0; }
Output Answer:
0133F9C0
0133F9C0
0133F9C1
0133F9C0
0133F9C4
Summary: The type of pointer determines how many bytes are being added or subtracted (on the memory address).
Dereference of pointer
int main() { int n = 0x11223344; char *pc = (char *)&n; int *pi = &n; *pc = 0; *pi = 0; return 0; }
Supplementary Note: The byte size of the dereference depends on the pointer type, and char * is one byte int * is four bytes. Computers generally store data on a small end.
The dereference of a pointer is also known as indirect access to the pointer, because the pointer stores the address of a variable, the dereference of the pointer finds the value stored in the address by the saved address.
For example, in the code above, PI holds the address of n, so *pi is an indirect access to the value of n. However, pc is a bit different, n is strongly converted to (char) type, char * holds a byte, int is four bytes, and according to the small-end storage principle, the original n is 0x11223344, but the char dereference is a byte, so it can only be accessed to 44.
Summary: The type of pointer determines how much privilege (a few bytes of operation) you have when you dereference the pointer. For example, a char's pointer dereference can only access one byte, while an int's pointer dereference can access four bytes.
Field Pointer
Concept: A field pointer is a pointer pointing to a location that is unknown (random, incorrect, and not explicitly restricted)
- Pointer not initialized
int main() { int *p;//Local variable pointer uninitialized, defaulting to random value *p = 20; return 0; }
- Pointer cross-border access
int main() { int arr[10] = {0}; int *p = arr; int i = 0; for(i=0; i<=11; i++) { //p is the field pointer when it points beyond the range of the array arr *(p++) = i; } return 0; }
How to Avoid Wild Pointer
- Pointer Initialization
- Be careful pointer is out of bounds
- Pointer points to space release even if NULL is set
- Check validity before pointer is used
Pointer and Array
Generally speaking, the array name holds the address of the array element. Since it is an address, you can use a pointer instead of an array name.
int arr[10] = {1,2,3,4,5,6,7,8,9,0}; int *p = arr;//p stores the address of the first element of the array
If the array name is replaced by a pointer, then the access to the array can be accessed by the pointer in two ways.
int main(){ int arr[10]={ 1,2,3,4,5,6,7,8,9,0}; int *p = arr; printf("%d \n",*(p+1)); printf("%d \n",p[1]); }
We can see that the above code shows both p[1] and * (p+1) output results of 2; So there are two ways to access arrays by pointer, one is to access arrays by dereferencing the pointer and adding or subtracting the pointer, the other is to access arrays directly by replacing the array name with the pointer name plus square brackets, both of which are feasible. So p+i actually calculates the address labeled I by the array arr. Then we can access the array directly through the pointer.
summary
Pointers are difficult chapters. The above is only about the basic knowledge of pointers. There are various types of pointers in depth, such as arrays of pointers, array pointers, function pointers, arrays of function pointers, pointers to arrays of function pointers, etc. There are also memory-related data storage and so on. The key point of the pointer is to clarify the memory address so that we can understand the meaning of the pointer more clearly.