C Language pointers are interesting . Through pointer , It can be simplified C Execution of programming tasks , There are still some tasks , Such as dynamic memory allocation , Cannot execute without a pointer . therefore , Want to be an excellent C
programmer , It is necessary to learn pointers . Let's get straight to the point

<> catalogue

1. What is the pointer
2. Pointer and pointer type
3. Field pointer
4. Pointers and arrays
5. Pointer array
6. Remember to give a compliment before you leave

<>1. What is the pointer ?

C In language , Variables are stored in memory , Memory is actually an array of ordered bytes , Each byte has a unique memory address .CPU
Locate the address of a specified data object stored in memory through memory addressing . here , A data object is a numeric value or string of a specified data type stored in memory , They all have their own address , The pointer is the variable that holds the address . in other words :
A pointer is a variable that holds the address of a variable .
Simple can be understood as :

* A pointer is the number of the smallest cell in memory ( It's just a byte ), That's the address
* Pointer in spoken English , Usually refers to pointer variables , Is a variable used to store the memory address
summary : A pointer is an address , In colloquial language, pointer usually refers to pointer variable
We can see the memory distribution of the following code #include<stdio.h> int main() { int a[10] = { 0 }; int i = 0;
for (i = 0; i < 10;i++) printf("%p\n", &a[i]); return 0; }
The output result is

Because the array is integer , So the difference between every two elements 4 byte

<>2. Pointer and pointer type

Example code
int num = 10; p = #
Want to &num(num Address of ) Save to p in , We know p Is a pointer variable , What is its type ?
We give the pointer variable the corresponding type .
char *pc = NULL; int *pi = NULL; short *ps = NULL; long *pl = NULL; float *pf =
NULL; double *pd = NULL;
You can see it here , Pointers are defined by : type + * .
actually :
char* Type pointer is used to store char Address of type variable .
short* Type pointer is used to store short Address of type variable .
int* Type pointer is used to store int Address of type variable .

Look at the following code
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 big the pointer is to go one step forward or backward ( distance ).
as int Take a step 4 Bytes ,char Take a step 1 Bytes …
Pointer dereference
#include <stdio.h> int main() { int n = 0x11223344; char *pc = (char *)&n; int
*pi = &n; *pc = 0; // Focus on observing the changes of memory during debugging . *pi = 0; // Focus on observing the changes of memory during debugging . return 0; }
summary :
The type of pointer determines , How much permission do you have when dereferencing pointers ( Can operate several bytes ).
such as : char* Pointer dereference can only access one byte , and int* The dereference of the pointer can access four bytes .

<>3. Field pointer

concept : The wild pointer is that the position pointed by the pointer is unknown ( Random , Incorrect , There are no clear restrictions )
(1) Origin of wild pointer
a. Pointer not initialized
#include <stdio.h> int main() { int *p;// Local variable pointer not initialized , Default to random value *p = 20; return 0; }
b. 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 pointer points to a range outside the array arr When the range of ,p It's a wild pointer *(p++) = i; } return 0; }
c. The space pointed to by the pointer is released
(2) How to avoid wild pointer

* Pointer initialization
* Be careful that the pointer is out of bounds
* Pointer to space release NULL
* Avoid returning the address of a local variable
* Check validity of pointer before use
<>4. Pointers and arrays

Upper code
#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 .( **sizeof( array )** and & array except Outside )

Then we can also 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; }

<>5. Pointer array

Pointer array is an array as its name suggests , Is an array used to store pointers
We already know about shaping arrays , Character array
int arr1[5]; char arr2[6];

What about pointer arrays ?
int* arr3[5];// What is it? ?

arr3 Is an array , There are five elements , Each element is an integer pointer .

Technology