Word Count: 6000 Content Quality Index: ⭐⭐⭐⭐⭐
Abstract: Have you ever wondered where the functions and variables you write in Keil for microcontrollers end up? We often talk about the five areas of memory, but what exactly are those five areas? Where are they located on the chip? And why is it that after learning about pointers and structures in C, you still struggle with pointers to structures in 32-bit microcontrollers? If you have these questions, let’s explore them today!



1. Variables
1. Define an int type variable. By printing, we can see that the address where this variable is stored is: 0x20000000. This also confirms that the starting address of our memory is 0x20000000. Our defined variable ‘value’ is stored here.


2. Now define another variable.
By printing, we can see that this variable is stored at the address: 0x20000004. Since the int type occupies 4 bytes in memory, the second variable is stored at 0x20000004.


In summary, the two defined variables in memory look like this:
0x2000 0000 stores 0
0x2000 0004 stores 1

2. Pointer Variables
Defining a pointer is essentially the same as defining a variable, except that a * is placed in front of the variable name.
Next, let’s define an int type pointer variable named p. Someone might ask, why does adding a * in front of the variable name make it a pointer?
Answer: It’s a convention established by the C language developers.
Defining a pointer is the same as defining a variable, and you can define various types.
Remember this phrase:
“
#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 variable value to this pointer
printf("Address of a: %p\n",p);// Print the address this pointer points to
while(1)
{
}
}


#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;// Copy the address of variable value to this pointer
q=&value3;// Copy the address of variable value3 to this pointer
printf("Address of a: %p\n",q);// Print the address this pointer points to
while(1)
{
}
}
3. What Are Pointers Used For?
#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;// Copy the address of variable value to pointer variable p
printf("Address of a: %d\n",value);
printf("Address of b: %d\n",*p);
while(1)
{
}
}



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


4. Function Pointers
#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);// Serial port initialization
delay_init();
p=function();// Call the function, which assigns the address of value to p
printf("Address1 of a: %p\n",&value);// Print the address of value
printf("Address2 of a: %p\n",p);// Print the address represented by p
while(1)
{
}
}


#include "sys.h"
#include "led.h"
#include "delay.h"
#include "usart.h"
int value = 0;
int *p;// Define a pointer
int *q;// Define another pointer
int main(void)
{
uart_init(115200);// Serial port initialization
delay_init();
p=&value;// Assign the address of value to p
q=p;// Assign the address represented by p to q
printf("Address1 of a: %p\n",&value);// Print the address of value
printf("Address2 of a: %p\n",q);// Print the address represented by q
while(1)
{
}
}


#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)
{
}
}
#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)
{
}
}
#include "sys.h"
#include "led.h"
#include "delay.h"
#include "usart.h"
void (*fun)(int a);
void function(int value)
{
printf("value= %d\r
",value);
}
int main(void)
{
uart_init(115200);// Serial port initialization
delay_init();
fun = function;// Assign function to fun
fun(520);// fun is equivalent to function
while(1)
{
}
}


#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;// Assign function to fun
res = fun(520);// fun is equivalent to function
printf("res = %d",res);
while(1)
{
}
}


#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 the array name to pointer variable p, making p point to the first address of array temp
printf("value0 = %d\r\n",*p); // p represents the address of the first element of the array
printf("value1 = %d\r\n",*(p+1));// p+1 represents the address of the second element of the array
printf("value2 = %d\r\n",*(p+2));// p+2 represents the address of the third element of the array
printf("temp[0] = %d\r\n",p[0]);// p[0] is equivalent to temp[0]
printf("temp[1] = %d\r\n",p[1]);// p[1] is equivalent to temp[1]
printf("temp[2] = %d\r\n",p[2]);// p[2] is equivalent to temp[2]
while(1)
{
}
}


5. Function Parameters as Pointers
#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)
{
}
}



1
“Methods for Job Hunting in Embedded Software Positions (Detailed)”
2
“Meituan Embedded Software Position Written Test Questions”
3
“How to Transition from Microcontrollers to Embedded Linux After 6 Years?”


