Pay attention to one button three times (don't be sure next time)
Hello, bald men, let's learn the pointer together today!
Key points of this chapter
- What is the pointer
- Pointer and pointer type
- Field pointer
- Pointer operation
- Pointers and arrays
- Secondary pointer
- Pointer array
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 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 visualization is called "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.
That corresponds to the code:
#include <stdio.h> int main() { int a = 10;//Open up a space in memory int *p = &a;//Here we take the address of variable a, and we can use the & operator. //Store the address of a in the p variable, and p is a pointer variable. return 0; }
Summary:
A pointer is a variable used to store an address.
(the values stored in the pointer are treated as addresses).
The problem here is:
- How big is a small unit? (1 byte)
- 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 a 32-bit machine, assuming that there are 32 address lines, it is assumed that each address line generates an electrical signal positive / negative (1 or 0) when addressing
Then the address generated by the 32 address lines will be:
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000001
...
11111111 11111111 11111111 11111111
There are 32 addresses to the power of 2.
Each address identifies a byte,
Then we can address the idle 4G.
In the same way, if the 64 bit machine is given 64 address lines, how much space it can address, calculate it yourself.
Here we understand:
On a 32-bit machine, the address is 32 zeros or 1s to form a binary sequence, so the address must be stored in 4 bytes, so the size of a pointer variable should be 4 bytes.
On a 64 bit machine, if there are 64 address lines, the size of a pointer variable is 8 bytes to store an address.
Summary:
- Pointers are used to store addresses, which uniquely identify an address space.
- The size of the pointer is 4 bytes on 32-bit platforms and 8 bytes on 64 bit platforms.
Pointer and pointer type
Here we are discussing: we all know the types of pointers. Variables have different types, such as integer, floating point, etc. Does the pointer have a type? The answer is yes!
char *pc = NULL; int *pi = NULL; short *ps = NULL; long *pl = NULL; float *pf = NULL; double *pd = NULL;
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. Pointer of type int * is used to store the address of variable of type int.
What is the meaning of pointer type?
#include<stdio.h> 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; }
Summary:
The type of pointer determines how far the pointer moves forward or backward.
Dereference of pointer
#include <stdio.h> int main() { int n = 0x11223344; char *pc = (char *)&n; int *pi = &n; *pc = 0; *pi = 0; return 0; }
Focus on observing memory changes during debugging.
Summary:
The type of pointer determines how much permission (several bytes can be operated) you have when dereferencing the pointer. For example, the dereference of char * pointer can only access one byte, while the dereference of int * pointer can access four bytes.
Field pointer
Concept: a wild pointer means that the position pointed to by the pointer is unknown (random, incorrect, and without explicit restrictions)
Origin of wild pointer
- Pointer not initialized
#include <stdio.h> int main() { int *p;//Local variable pointer is uninitialized and defaults to random value *p = 20; return 0; }
- Pointer out of bounds access
#include <stdio.h> int main() { int arr[10] = {0}; int *p = arr; int i = 0; for(i=0; i<=11; i++) { //When the range pointed to by the pointer exceeds the range of array arr, p is a wild pointer *(p++) = i; } return 0; }
- The space pointed to by the pointer is released
I will explain this later when developing dynamic memory
To put it simply, when you use a space, you use a pointer to point to the space. When you no longer use the space, you should free() drop the pointer and set the pointer to NULL. If you don't do so, your pointer will still point to the position. It is a wild pointer. If you operate improperly, it will cause dangers such as illegal access.
How to avoid wild pointer
- Pointer initialization
- Be careful that the pointer is out of range
- Pointer to space release even if set to NULL
- Check the validity of the pointer before use
#include <stdio.h> int main() { int *p = NULL; //.... int a = 10; p = &a; if(p != NULL) { *p = 20; } return 0; }
Pointer operation
Pointer ± integer
Pointer pointer
Relational operation of pointer
Pointer ± integer
#define N_VALUES 5 float values[N_VALUES]; float *vp; //Pointer + - integer; Relational operation of pointer for (vp = &values[0]; vp < &values[N_VALUES];) { *vp++ = 0; }
Pointer pointer
int my_strlen(char *s) { char *p = s; while(*p != '\0' ) p++; return p-s; }
Relational operation of pointer
for(vp = &values[N_VALUES]; vp > &values[0];) { *--vp = 0; }
Standard provisions:
A pointer to an array element is allowed to be compared with a pointer to the memory location after the last element of the array, but it is not allowed to be compared with a pointer to the memory location before the first element.
Why?
As shown in the figure, for example, an array has 9 elements, and the subscripts are 0 – 8. Pointer p2 and pointer p3 are allowed to be compared, but pointer p1 and pointer p2 are not allowed to be compared.
Although pointer p3 is out of bounds, the address still increases according to the original type size, but the computer will quietly put a 9 to indicate that there are 9 elements in the following array. Therefore, the address of pointer p1 is uncertain and cannot be compared with other pointers.
Pointers and arrays
What is the array name? Let's take an example:
#include <stdio.h> int main() { int arr[10] = {1,2,3,4,5,6,7,8,9,0}; printf("%p\n", arr); printf("%p\n", &arr[0]); return 0; }
It can be seen that the array name and the address of the first element of the array are the same.
Conclusion:
The array name represents the address of the first element of the array.
Then it is feasible to write code like this:
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
Since the array name can be stored as an address in a pointer, it is possible for us to use the pointer to access an element.
For example:
#include <stdio.h> 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.
Then we can access the array directly through the pointer.
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("%d ", *(p + i)); } return 0; }
Secondary pointer
Pointer variable is also a variable. If it is a variable, it has an address. Where is the address of pointer variable stored? This is the secondary pointer.
The operations of the secondary pointer are:
*ppa finds pa by dereferencing the address in ppa, * ppa actually accesses PA
int b = 20; *ppa = &b;//Equivalent to PA = & B;
**ppa first finds pa through * ppa, and then dereferences PA: * PA, then a is found
**ppa = 30; //Equivalent to * pa = 30; //Equivalent to a = 30;
Pointer array
Is a pointer array a pointer or an array?
Answer: array. Is an array of pointers.
Array, we already know integer array, character array.
int arr1[5]; char arr2[6];
What about pointer arrays?
int* arr3[5];//What is it?
arr3 is an array with five elements, each of which is an integer pointer.
That's all for this issue. I'll see you next issue
If there is any mistake, welcome to communicate with us
It will continue to output in the future, and continue to pay attention to Zhou Wang
Remember to pay attention to the third company