The test code is as follows
#include "reg51.h"
#include "stdio.h"
#include "intrins.h"
typedef void (*p_fun)(unsigned int*); //void (*p_fun)(unsigned int*);
p_fun p_fun_a; //Function pointer variable
void p_fun_parameter(p_fun p_fun_1,unsigned int *test_b) //function pointer as parameter
{
p_fun_1(test_b);
}
char putchar(char s)
{
SBUF=s;
while(TI==0)
{
_nop_();
}
TI = 0;
return 0;
}
void test_return(unsigned int *i)
{
while((*i)--)
{
if((*i)<5)
{
return; //When (*i)=4, exit the test_return function
}
}
*i=(*i)+1;
}
void test_break(unsigned int *i)
{
while((*i)--)
{
if((*i)<5)
{
break; //When (*i)=4, exit the while loop and continue to execute *i=*i+1;
}
}
*i=*i+1;
}
void test_continue(unsigned int *i)
{
while((*i)--)
{
if((*i)<5)
{
continue; ///When (*i)=4, exit this loop and continue to execute the next while loop
*i=*i+1;
}
}
*i=*i+1;
}
void test_priority(unsigned int *i)
{
while(*i--)
{
if(*i<5)
{
break;
}
}
*i=*i+1;
}
unsigned int a=10,b=10,c=10,d=10;
void main()
{
void (*test)(unsigned int*);
p_fun_parameter(test_return,&a);
printf("a=%drn",a);
p_fun_parameter(test_break,&b);
printf("b=%drn",b);
test=test_continue;
test(&c);
printf("c0=%drn",c);
test_priority(&d);
printf("d=%drn",d);
printf("c1=%drn",c);
while(1);
}
The test results are as follows
The address of a in memory is 0x22
The address of b in memory is 0x24
The address of c in memory is 0x26
The address of d in memory is 0x28
(The above addresses are the addresses assigned by the microcontroller. Different programs and CPUs assign different addresses.)
Question 1: Why is the answer to c 0 the first time?
When analyzing this problem, we need to pay attention to while((*i)--), note that it is a signed number, and that (*i)-- is used first and then decremented.
Question 2: Why is the second output of c 1?
When analyzing this problem, we need to pay attention to while(*i--), pay attention to the priority of-- and the pointer, * and-- have the same priority, and are combined from right to left, so first the pointer is reduced by 1, and then the content is taken.
As for *i+3, * has a higher priority than +, so the content is taken first and then three is added.
Array names are different from pointers, so the * operation cannot be performed on array names. When passed as actual parameters, the first address is passed. At this time, the * operation can be performed on the formal parameters inside the function, and it can also be understood as an array name, degenerating into a pointer.
Summarize:
The function of return is to exit the function where the loop body is located, which is equivalent to ending the function.
The function of break is to end the loop, jump out of the loop body, and execute the subsequent program, which is the same as the break under the case in switch.
The function of continue is to end this loop and proceed to the next loop;
Previous article:Notes on using function pointers in Keil C51
Next article:C language_floating point_IEEE 754 standard_MCU_floating point precision
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- K210 face recognition environment construction process
- Charge up! The latest generation of ACF UCC28782 ultra-small fast charging adapter!
- Proteus simulation experiment of single chip microcomputer communication
- 【TI recommended course】#Power system design tools#
- Question about username
- NeoPixel simulates fluid physics motion
- BMP library management based on FPGA.pdf
- Making some preparations for the flag I set up on the forum this year (first time shopping at the ghost market)
- Open source scientific calculator based on STC32G
- FPGA---DDS Sine Wave Generator