An article is enough to understand where the pointer is sacred

The following article comes from: public_ Numerous_ Open source electronic network
Read more technical articles, please scan the code and pay attention

What is a pointer:

Do you really understand? Do you know how computer memory is stored? If you really know what pointers are, you will know how computer memory is stored.
First of all, before we understand the pointer, we must understand how the computer memory is stored. The general 32-bit computer divides the memory into bytes, and the general 32-bit machine int The type is 32 bits, that is, 4 bytes. There are 8 bytes in a byte bit Data, a knowledge point that must be known, is shown in the following figure:

As shown in the figure above, one byte is 8 bit Therefore, in order to distinguish each byte of memory, the computer arranges numbers for them, and these numbers are addresses. For example, in the school dormitory, there are eight people living in each dormitory, and the dormitory must have a dormitory number. Therefore, the principle of a dormitory staff whose hostess will search the dormitory number is similar.

As can be seen from the above figure, generally speaking, int variables have 4 bytes, so each address is offset by 4 bytes. If the pointer P points to the address 0x20000004, the value stored in the address is 0x82.

What is the C language pointer

Above, we have learned how the computer memory stores and reads the value of the address. Obviously, Xiaobian refers to a 32-bit computer to facilitate us to learn MCU, because most MUC s are also 32-bit.
C language pointer can simplify some tasks executed by C programming tasks, such as dynamic memory allocation. If there is no pointer, it cannot be executed. Therefore, it is necessary to learn pointers as a C programmer or embedded engineer.
The address of data in memory is also called pointer. If a variable stores a pointer to data, we call it pointer variable. When learning C language, variables have a memory location, so they can use the "&" operator to access the address to represent an address in memory. As shown in the following source code:

#include <stdio.h>

 
int main ()
{
   /* int Variable of type */
   int  lv_variate = 10;
   /* Pointer variable */
   int  *ip_variate; 
   /* Store LV in pointer variable_ Variable address */
   ip_variate = &lv_variate;  
 
   printf("lv_variate Address of variable: %p\n", &lv_variate  );
 
   /* Address stored in pointer variable */
   printf("ip_variate Address of pointer variable storage: %p\n", ip_variate );

    /* The address of the pointer variable */
   printf("ip_variate Address of pointer variable: %p\n", &ip_variate );

   /* Accessing values using pointers */
   printf("*ip_variate Value of variable: %d\n", *ip_variate );
 
   return 0;
}
The serial port mode assistant outputs their printing information, as shown below:
lv_variate Address of variable: 20000784
ip_variate Address of variable storage: 20000784
ip_variate Address of pointer variable: 20000780
*ip_variate Value of variable: 10
How do we understand the above output information?, You can use the schematic diagram to analyze the above information, as shown in the figure:

Summary:
① Variable name, function name, string name and array name are essentially the same. They are mnemonics of addresses
② "&" operator access address
③*ip_ Variable is to read the value corresponding to the address stored in the pointer variable (value 10 of the address of 20000784)
④ip_ Variable reads the address of the pointer variable store
Tips:
If the program is compiled and linked, use * IP_ If you want to change, you must first obtain the variable IP through the address 20000780_ The value of variable itself, which is the variable Lv_ The address of variable, and then get the variable LV through this value_ Variable data, there are two steps before and after; Use LV instead_ If variable, its data can be obtained directly through address 20000784, which requires only one step of operation.
It can be said that using pointers is to obtain data indirectly, and using variable names is to obtain data directly.
What is a secondary pointer:
Above, we understand what is a pointer and the operation of a primary pointer. If a pointer points to another pointer, we call it a secondary pointer or a pointer to a pointer.
A secondary pointer is a pointer to a primary pointer. The secondary pointer points to the primary pointer, that is, the memory address of the primary pointer is stored in the secondary pointer.

Use of secondary pointer:

int  main( void )
{
     int  a = 10;                           		//Declare a variable a
     int  *p = &a;                          		//Declare pointer p to variable a
     int  **q = &p;                         		//Declare the secondary pointer q and point to the primary pointer p
     printf ( "a = %d\n" ,a);               	//Print the value of variable a
     printf ( "a Address of&a=%p\n" ,&a);        	//Print the address of variable a
     printf ( "p = %p\n" ,p);               	//Print the value of p
     printf ( "p Address of&p=%p\n" ,&p);        	//Print p's address
     printf ( "p Dereference of*p=%d\n" ,*p);      	//Print p dereference
     printf ( "q = %p\n" ,q);               	//Print the value of q
     printf ( "q Address of&q=%p\n" ,&q);        	//Print q's address
     printf ( "q Dereference of*q=%p\n" ,*q);      	//Print q's dereference
     printf ( "q Double dereference of**q=%d\n" ,**q);	//Double dereference of printed q
     return  0;
}
Execution results:
a = 10
a Address of&a=20000788
p = 20000788
p Address of&p=20000784
p Dereference of*p=10
q = 20000784
q Address of&q=20000780
q Dereference of*q=20000788
q Double dereference of**q=10
How do we understand the above output information?, You can use the schematic diagram to analyze the above information, as shown in the figure:

Summary:

① Variable name, function name, string name and array name are essentially the same. They are mnemonics of addresses
② "&" operator access address
③ q read the address stored in the pointer variable (0x20000784)
④ * q read the corresponding value of the address stored in the pointer variable (that is, the address stored in the primary pointer variable (0x20000788))
⑤ * * q read the value of the corresponding address of the corresponding value of the address stored in the pointer variable (that is, the value (10) corresponding to the address (0x20000788) stored in the primary pointer variable)

Keywords: C Back-end Embedded system

Added by gmwebs on Thu, 04 Nov 2021 09:21:02 +0200