Read the essence of pointers in one article

Address: The so-called address is an integer number (constant). The address does not contain any type information.
Pointers: Pointers are divided into pointer constants and pointer variables. When referring to pointers alone, they usually refer to pointer constants. Among them:
pointer constant = address value (constant) + type information
pointer variable = integer variable + type information

Variable = * Pointer
Pointer = & Variable
Type information can be achieved through forced type conversion, which is the familiar (< Type >) usage. The change of the address value will be uniformly converted into an ordinary integer.
Pointer constant = integer constant + type information
That is:
pointer constant = (<type information> *) integer constant The
reverse is also true:
integer constant = pointer constant-type information
that is:
integer constant = (unsigned int) pointer constant

Application example:
Create a global variable:

#define s_wMyVariable    (* (( uint32_t *) 0x12345678))

Define a macro to mass-produce global variables for us:

#define __VAR(__TYPE, __ADDR)     (*(__TYPE *) (__ADDR))

It is also very convenient to use, for example:

__VAR(  float, 0x20004000  ) = 3.1415926

Universal type conversion:

#define CONVERT(  __ADDR,   __TYPE )            \
    __VAR(  (__TYPE),  (uint32_t) (__ADDR)  )

Examples of universal type conversion applications:
For example, we can directly intercept a certain section of the byte array and access it as a certain type of variable:

//! 某数据帧解析函数
void command_handler(  uint8_t *pchStream, uint16_t hwLength  )
{
    
    
    // offset 0, uint16_t  
    uint16_t hwID = CONVERT( pchStream, uint16_t);  
    // offset 4, float
    float fReceivedValue = CONVERT( &pchStream[ 4 ], float ) ;
    ...
}

Please forget the pointer:
If you are a pointer, then please forget everything you learned before. Remember one sentence: a pointer is just an integer variable with weird usage, specially used to store the address value of the variable . The pointer type is used to deceive the compiler, I am a smart human, I manipulate types, I am not a stupid compiler.
  Corollary: Because the nature of the pointer variable is an integer variable, the pointer to the pointer is just an ordinary pointer to the ordinary integer variable. Therefore, the pointer to the pointer does not exist—only ordinary pointers in the world—only There are integer variables with weird usage, specially used to save the address value of the target variable.
  Corollary: there is no pointer to pointer to pointer in the world...

Give me an integer and make my own variables. The numerical calculation of the pointer is too bad? Convert into integers, add, subtract, multiply and divide, just round up.

This article comes from the official account : Bare Metal Thinking
Original Location: Please, don’t worry about pointers anymore (1) ——Universal Conversion Formula

Guess you like

Origin blog.csdn.net/sinat_31039061/article/details/108079048