Abstract: We have not thought we use keil to write the single-chip code, your function ah, variables ah finally put in where? What are the five regions of memory we keep talking about? Where the hell is it on the chip? And why do you learn the C language pointer and structure 32 microcontroller inside the structure pointer content or not clear? If you have these problems, I will take you to research today!

This picture should be familiar to those who have learned STM32 microcontroller. The STM32 chip we see has been packaged and finished, mainly composed of core and on-chip peripherals. If with the computer analogy, the core and peripherals are like the CPU on the computer and the motherboard, memory, graphics card, hard disk relationship. The chip and peripherals are connected by various buses. Connected to the controlled bus are FLASH, RAM, and on-chip peripherals, all arranged in a 4GB address space. The above pictures are the memory address map of STM32F40XXX series MCU.

Our code is in Flash (0x8000000~0x80FFFFF). The code is the various functions that you write, and the variables declared in the program or program are stored in RAM. Local variables are released when the function is finished running, and global variables are released when the program is finished running.

The variables that the CPU uses are stored in RAM, and if you ask me what RAM is, RAM is a chip. This is the SRAM area of Block1 in the image above. The CPU is connected to the RAM chip through wires, which can then store and read data into the RAM chip. First of all, RAM needs to have a starting address. For STM32 MCU, the starting address is 0x20000000. Why do YOU want to specify the address? Only the address CPU is good for data storage, if there is no address, a few blind storage, a few blind take……

1, variables,

1. Define a variable of type int (0x20000000). This also proves that the first address of our memory is 0x20000000. The value variable that we defined is right there.

2. Define another variable

You can see from the print that the variable is stored at the address 0x20000004. Since int takes up four bytes in memory, the second variable is stored at 0x20000004.

In summary, the two variables defined in memory look like this.

0x2000 0000 stores 0. 0x2000 0004 stores 1

2. Pointer variables

Defining a pointer is the same as defining a variable, except that the variable name is preceded by an *

Here we define a pointer variable of type int named p. And then one might ask, why is a pointer to a variable name preceded by an asterisk?

A: It’s stipulated by the C guys.

You can define a pointer just like you can define a variable.

Then remember one sentence:

Pointer this variable is to store the address of the variable!

Pointer this variable is to store the address of the variable! Pointer this variable is to store the address of the variable!

So assigning a value to a pointer naturally gives 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;// Copy the address of the variable value to the pointer
	printf("Address of a: %p\n",p);// Print the address to which this pointer points
  while(1) {}}Copy the code

A pointer variable of any type should be assigned its address. Let’s say we define a char

#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);// Initialize the serial port
	delay_init();
	
	p=&value;// Copy the address of the variable value to the pointer
	q=&value3;// Copy the address of the variable value to the pointer
	
	printf("Address of a: %p\n",q);// Print the address to which this pointer points
    while(1) {}}Copy the code

What is the use of Pointers by those who prescribe C?

3. What are Pointers for?

1. Let’s use and feel the pointer first, and then see what it’s useful for. We assigned the address of a variable to a pointer, and then the C guys dictated that. *{pointer variable name} : represents the variable to which the pointer points.

What does that mean?

P =&value, p records the address of the variable value, and *p represents 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);// Initialize the serial port
	delay_init();
	
	p=&value;// copy the address of value to the pointer variable p
	
	printf("Address of a: %d\n",value);
	printf("Address of b: %d\n",*p);
  while(1) {}}Copy the code

Some people think…… Is this it?

This is not farting with your pants down

Superfluous?

In fact, I also think so at the beginning……

* p=XXXX * p=XXXX * p=XXXX * p=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);// Initialize the serial port
	delay_init();
	
	p=&value;// copy the address of value to the pointer variable p
	printf("value of a: %d\n",value);
	
	*p=520;	
	
	printf("value of b: %d\n",value);
    while(1) {}}Copy the code

Still don’t see any use in the pointer? Don’t worry. Just finish the basics first. Without the most basic knowledge reserve is not possible, because thick accumulation and thin hair!

Ever see a function that returns a pointer?

Function pointer

Look at it first, and if you don’t understand it, keep reading

#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;// Return the address of value
}
	
int main(void)
{	
	uart_init(115200);// Initialize the serial port
	delay_init();
	
	p=function();// Call a function that assigns the address of value to p
	
	printf("Address1 of a: %p\n",&value);// Prints the address of value
	printf("Address2 of a: %p\n",p);// Print the address represented by p
	
    while(1) {}}Copy the code

Many people have used functions that return int, char, etc., but with a * after int, char

Probably not used for beginners. See below, it’s actually assigning between Pointers. The following is to assign the address represented by p(int*) to q

Variables can be assigned to each other, and Pointers can be assigned to each other.

Function returns a pointer to int* and assigns it to p

#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);// Initialize the serial port
	delay_init();
	
	p=&value;// assign the address of value to p
	q=p;// give the address p represents to q
	
	printf("Address1 of a: %p\n",&value);// Prints the address of value
	printf("Address2 of a: %p\n",q);// Print the address represented by p
	
  while(1) {}}Copy the code

But again, what’s 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(a)
{
  printf("zhiguoxin\n");
}
int main(void)
{	
	uart_init(115200);// Initialize the serial port
	delay_init();	
	
  function();	
    
  while(1) {}}Copy the code

But I’ve never seen anything like this

#include "sys.h"
#include "led.h"
#include "delay.h"
#include "usart.h"

void (*fun)();

void function(a)
{
  printf("zhiguoxin\n");
}

int main(void)
{	
	uart_init(115200);// Initialize the serial port
	delay_init();
	
	fun = function;
  fun();
		
  while(1) {}}Copy the code

Function Pointers are used here

Remember one thing

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 is an address, the address should be assigned to a pointer. Since it is the address of a function, the pointer we define must be of a function type.

The above function void function() is a function with no return value and no parameters. Void (* pointer variable name) (); Void (*fun)(); Fun is a pointer to a function type. It is a function pointer with no return value and no parameters.

We can assign this function to this pointer variable. That’s fun=function up there. So this function pointer represents that function fun is equal to function. So call fun(); This is equivalent to calling function().

What if the function takes arguments? Easy. Let’s add it when it’s available

#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);// Initialize the serial port
	delay_init();
	
	fun = function;// Assign function to fun
  fun(520);// Fun is the same as function
		
  while(1) {}}Copy the code

What if the function returns a value? That seems to add

#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);// Initialize the serial port
	delay_init();
	
	fun = function;// Assign function to fun
  res = fun(520);// Fun is the same as function
		
	printf("res = %d",res);
  while(1) {}}Copy the code

So just to summarize

Pointers are basically the ones above, and Pointers are used to record the address of a variable. Or do the transfer between addresses.

Ampersand means to fetch the address.

* means to fetch data. &{variable name} : fetch the address of this variable. *{pointer variable name} : to fetch the value of the address represented by this pointer

Here are some of the more common applications. Assign the address of the array to the pointer, and then manipulate the array with the 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);// Initialize the serial port
	delay_init();
	
  p=temp;// Assign the name of the array to the pointer variable p, which points to the beginning of the array temp
		
	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]等同于temp[0]
	printf("temp[1] = %d\r\n",p[1]);//p[1]等同于temp[1]
	printf("temp[2] = %d\r\n",p[2]);//p[2]等同于temp[2]	
  while(1) {}}Copy the code

The parameter to the 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);// Initialize the serial port
	delay_init();
	
	function(temp);

  while(1) {}}Copy the code

The basic Pointers above can be practiced a few times. The real use of Pointers lies in the encapsulation of code. It may not work for beginners, but when you become a real developer. You’ll find that wrapping up a lot of functionality and leaving interfaces to call is essential in the future.

When encapsulation will use a lot of Pointers, function Pointers, structure Pointers, etc., how to say! 90% of programmers type letters and write code. When you begin to encapsulate, you write ideas, but it takes a certain amount of basic knowledge to get there.