abstract
: Have you ever thought about our use keil Write MCU code , Your function , Where are the variables in the end ? We have been talking about five areas of memory , What are the five districts ? Where exactly is the chip ? And why did you finish C Language pointers and structs ,32 MCU inside the pointer on the structure of the content or not clear ? If you have these questions , I'll take you to study today !

I learned this picture STM32 SCM's little friends should not be unfamiliar , What we see STM32 The chip is already packaged , It is mainly composed of kernel and peripherals . If the analogy with computer , The kernel and peripherals are just like those on a computer CPU And motherboard , Memory , Graphics card , The relationship between hard disks . Chips and peripherals are connected by various buses . What is connected to the controlled bus FLASH,RAM And peripherals on chip , These functions are arranged together in a single unit 4GB In the address space of . These are the pictures above STM32F40XXX Memory address map of Series MCU . Our code is placed in the Flash inside (0x8000000~0x80FFFFF). The code is all the functions you write , The variables declared in the program or are placed in the RAM in , Local variables are released when the function runs , Global variables are released after the program runs , It can be understood in this simple way .

CPU The variables used are stored in RAM Inside , Ask me RAM What is it ,RAM It's a chip . This is the picture above Block1 Of SRAM area .CPU It's through wires and wires RAM Chip connected , You can then go through the wires to RAM The chip stores and reads data . First of all RAM You need a starting address , about STM32 For SCM, the starting address is 0x20000000, Why do I have to specify the address . Only the address is specified CPU To store the data , If there's no address , Save a few , Pick up a few …

<>1, variable

1. It defines a int Variable of type , By printing, you can see that the address of this variable is :0x20000000. This also proves that the first address of our memory is 0x20000000. We define it value Here are the variables .

2. Define another variable

By printing, you can see that the address of this variable is :0x20000004. because int Type is occupied in memory 4 Bytes , So the second variable is stored in the 0x20000004 This place .

in summary , The two variables defined in memory are as follows .

0x2000 0000 What's in the address 0
0x2000 0004 What's in the address 1

<>2, Pointer variable

Defining a pointer is actually the same as defining a variable , It's just that there's one in front of the variable name *

Here's a definition int Pointer variable of type , The name of the variable is p. Then someone will ask , Why add a variable before its name * That's the pointer ?

answer : get C Language is what the guys say .

Defining pointers is the same as defining variables , You can then define various types of .

Then remember one sentence :

The pointer is the address of the stored variable !
The pointer is the address of the stored variable !
The pointer is the address of the stored variable !

So to assign a value to a pointer is to give it the address of the variable .
#include "sys.h" #include "led.h" #include "delay.h" #include "usart.h" int
value= 0; int value2 = 1; int *p; int main(void) { uart_init(115200); delay_init
(); p=&value;// Put the variable value Copy the address of to this pointer printf("Address of a: %p\n",p);// Print the address that this pointer points to
while(1) { } }

Generally, the address of a pointer variable of any type should be assigned . If you redefine a char type
#include "sys.h" #include "led.h" #include "delay.h" #include "usart.h" int
value= 0; int value2 = 1; int *p;// Define a pointer char value3=1; char *q; int main(void)
{ uart_init(115200);// Serial port initialization delay_init(); p=&value;// Put the variable value Copy the address of to this pointer q=&
value3;// Put the variable value Copy the address of to this pointer printf("Address of a: %p\n",q);// Print the address that this pointer points to while(
1) { } }
What are the rules C What's the use of the pointer made by the language boss ?

<>3, What's the use of a pointer ?

1. Let's feel the pointer first , And then what is the specific use of their own experience . We assigned the address of a variable to the pointer , And then do it C What's the rule of language .*{ Pointer variable name }
: Represents the variable that this pointer points to .

What do you mean ?

Refer to the following procedure p=&value, p Variables are recorded value Your address , then *p On behalf of value.
#include "sys.h" #include "led.h" #include "delay.h" #include "usart.h" int
value= 0; int *p;// Define a pointer int main(void) { uart_init(115200);// Serial port initialization delay_init(
); p=&value;// Put the variable value Copy the address of to the pointer variable p printf("Address of a: %d\n",value); printf(
"Address of b: %d\n",*p); while(1) { } }

Some people will think … That's it ???

It's not like taking off your pants and farting

carry coals to newcastle ???

Actually, that's what I thought at the beginning …

since * p On behalf of value
that * p=XXXX, No, it's equivalent to value=XXXX

Take a look at the following example
#include "sys.h" #include "led.h" #include "delay.h" #include "usart.h" int
value= 0; int *p;// Define a pointer int main(void) { uart_init(115200);// Serial port initialization delay_init(
); p=&value;// Put the variable value Copy the address of to the pointer variable p printf("value of a: %d\n",value); *p=520;
printf("value of b: %d\n",value); while(1) { } }

I still don't feel the use of the pointer ? take it easy , Let's finish the basic knowledge first . It's impossible without the most basic knowledge , Thin hair due to thick accumulation !

Have you ever seen a function whose return value is a pointer ?

<>4, Function pointer

Have a look first , If you don't understand, look down
#include "sys.h" #include "led.h" #include "delay.h" #include "usart.h" int
value= 0; int *p;// Define a pointer int *function(void) { return &value;// hold value Address return }
int main(void) { uart_init(115200);// Serial port initialization delay_init(); p=function();
// Call function , In fact, it's just value The address assigned to p printf("Address1 of a: %p\n",&value);// Printing value Your address
printf("Address2 of a: %p\n",p);// Printing p Address represented while(1) { } }

A lot of people have used the return value int,char And so on , But in int,char Add one after that *

It is estimated that it has not been used for beginners . Look at the picture below , In fact, it is the assignment between pointers . Here's how to p(int* Pointer to type ) The address of the representative is assigned to the q

Can variables assign values to each other , It's the same between pointers , You can assign values to each other .

In fact, it is the same as above , That function function The return value is a int * Pointer to type , And then it's assigned to p nothing more
#include "sys.h" #include "led.h" #include "delay.h" #include "usart.h" int
value= 0; int *p;// Define a pointer int *q;// Define a pointer int main(void) { uart_init(115200);
// Serial port initialization delay_init(); p=&value;// hold value The address assigned to p q=p;// hold p The representative's address is q printf(
"Address1 of a: %p\n",&value);// Printing value Your address printf("Address2 of a: %p\n",q);
// Printing p Address represented while(1) { } }

One more question , What is the name of the function ?

We all know how to call functions like this
#include "sys.h" #include "led.h" #include "delay.h" #include "usart.h" void
function() { printf("zhiguoxin\n"); } int main(void) { uart_init(115200);// Serial port initialization
delay_init(); function(); while(1) { } }

But have you ever seen one like this
#include "sys.h" #include "led.h" #include "delay.h" #include "usart.h" void (*
fun)(); void function() { printf("zhiguoxin\n"); } int main(void) { uart_init(
115200);// Serial port initialization delay_init(); fun = function; fun(); while(1) { } }

The function pointer is used here

Remember one sentence first

The function name is the address of the function !
The function name is the address of the function !
The function name is the address of the function !

Since it's the address , Then this address should be assigned to a pointer . Because it's the address of the function , So the pointer we define must also be of a function type .

The function above void function() Is a no return value , Functions without formal parameters . So we need to define a pointer type like this , In fact, it is void (* Pointer variable name , Write at will )
() . It says void (*fun)();
fun Is a pointer to a function type , Is a no return value , Function pointer without formal parameters .

We can assign this function to this pointer variable . It's on the top fun = function. Then this function pointer represents that function fun It's the same as function. So call
fun(); It's the same as calling function().

What if a function has formal parameters ? Easy to handle , If he has, we'll add it
#include "sys.h" #include "led.h" #include "delay.h" #include "usart.h" void (*
fun)(int a); void function(int value) { printf("value= %d\r\n",value); } int
main(void) { uart_init(115200);// Serial port initialization delay_init(); fun = function;
// hold function Assign to fun fun(520);//fun It's the same as function while(1) { } }

What if the function has a return value
#include "sys.h" #include "led.h" #include "delay.h" #include "usart.h" int res
; int (*fun)(int a); int function(int value) { return value; } int main(void) {
uart_init(115200);// Serial port initialization delay_init(); fun = function;// hold function Assign to fun res =
fun(520);//fun It's the same as function printf("res = %d",res); while(1) { } }

Let's summarize

In fact, the pointer is basically the one above , A pointer is used to record the address of a variable . Or do the transfer between addresses .

& Represents the address symbol .
* Representative data .
&{ Variable name } : That is to take out the address of this variable .
*{ Pointer variable name } : That is to take out the stored value in the address represented by this pointer

Here are some common applications . Assign the address of the array to the pointer , Then operate the array with a pointer
#include "sys.h" #include "led.h" #include "delay.h" #include "usart.h" char
temp[3]={1,2,3}; char *p; int main(void) { uart_init(115200);// Serial port initialization delay_init(
); p=temp;// Assign an array name to a pointer variable p,p Point to the array temp First address of printf("value0 = %d\r\n",*p);
//p Represents the address of the first data in the array printf("value1 = %d\r\n",*(p+1));//p+1 Represents the address of the second data in the array printf(
"value2 = %d\r\n",*(p+2));//p+2 Represents the address of the third data in the array printf("temp[0] = %d\r\n",p[0]);
//p[0] Equivalent to temp[0] printf("temp[1] = %d\r\n",p[1]);//p[1] Equivalent to temp[1] printf(
"temp[2] = %d\r\n",p[2]);//p[2] Equivalent to temp[2] while(1) { } }

<>5, The formal parameter of a function is a pointer
#include "sys.h" #include "led.h" #include "delay.h" #include "usart.h" char
temp[3]={1,2,3}; void function(char *value) { printf("value0 = %d\r\n",value[0])
; printf("value1 = %d\r\n",value[1]); printf("value2 = %d\r\n",value[2]); } int
main(void) { uart_init(115200);// Serial port initialization delay_init(); function(temp); while(1) {
} }

The basic knowledge of the above pointer can be practiced several times . The real application of pointer lies in the encapsulation of code . May not feel its effect for beginners , But when you become a real developer . You will find that many functions are encapsulated , Then it is necessary to set aside the interface to call .

A lot of pointers are used in encapsulation , Function pointer , Structure pointer, etc , How to put it? !90% Our programmers type letters , It's code . When you start packaging , You write about ideas , But it needs a certain amount of basic knowledge to achieve .

Technology