When I was learning ARM, I found that it was easy to confuse "pointer function" with "function pointer", so today, I want to clarify it once and for all, and I found some information and some summary from everyone, and I organized it here to share with you.
First, their definitions:
1. A pointer function is a function with a pointer, which is essentially a function. The function return type is a pointer of a certain type.
Type identifier*function name (parameter list)
int *f(x,y);
First of all, it is a function, but the return value of this function is an address value. The function return value must be accepted by a pointer variable of the same type, that is, a pointer function must have a function return value, and, in the calling function, the function return value must be assigned to a pointer variable of the same type.
express:
float *fun();
float *p;
p = fun(a);
Note the difference between pointer function and function pointer, and do not confuse them. The easiest way to distinguish is to see if the pointer * in front of the function name is contained in brackets (). If it is contained, it is a function pointer, otherwise it is a pointer function.
Let's talk about it in more detail! Please see below
Pointer function:
When a function declares that its return value is a pointer, it actually returns an address to the calling function for use in expressions that require a pointer or address.
Format:
Type specifier * Function name (parameters)
Of course, since what is returned is an address, the type specifier is usually int.
For example: int *GetDate();
int * aaa(int,int);
The function returns an address value, which is often used to return the address of a certain element in the array.
int * GetDate(int wk,int dy);
main()
{
int wk,dy;
do
{
printf(Enter week(1-3)day(1-4)n);
scanf(%d%d,&wk,&dy);
}
while(wk《1||wk》3||dy《1||dy》4);
printf(%dn,*GetDate(wk,dy));
}
int * GetDate(int wk,int dy)
{
static int calendar[3][4]=
{
{1,2,3,4},
{5,6,7,8},
{9,10,11,-1}
};
return &calendar[wk-1][dy-1];
}
The program should be easy to understand. The subroutine returns the address of an element in the array, and the output is the value at this address.
2. A function pointer is a pointer variable pointing to a function, that is, it is essentially a pointer variable.
int (*f) (int x); /* declare a function pointer */
f=func; /* Assign the first address of the func function to the pointer f */
A pointer to a function contains the address of the function, through which the function can be called. The declaration format is as follows:
Type specifier (*function name)(parameters)
Actually, this is not a function name, but a pointer variable name. This special pointer points to a function that returns an integer value. The pointer declaration should be consistent with the declaration of the function it points to.
The parentheses around the pointer name and pointer operator change the default operator precedence. Without the parentheses, it becomes a prototype declaration of a function that returns an integer pointer.
For example:
void (*fptr)();
Assigning the address of a function to a function pointer can be done in the following two ways:
fptr=&Function;
fptr=Function;
The address operator & is not required because a function identifier alone indicates its address. If it is a function call, it must also contain a parameter list enclosed in parentheses.
There are two ways to call a function through a pointer:
x=(*fptr)();
x=fptr();
The second format looks the same as a function call. However, some programmers prefer the first format because it explicitly indicates that the function is called through a pointer rather than a function name. Here is an example:
void (*funcp)();
void FileFunc(),EditFunc();
main()
{
funcp=FileFunc;
(*funcp)();
funcp=EditFunc;
(*funcp)();
}
void FileFunc()
{
printf(FileFuncn);
}
void EditFunc()
{
printf(EditFuncn);
}
The program output is:
FileFunc
EditFunc
The main difference is that one is a pointer variable and the other is a function. It is necessary to understand it clearly before using it correctly.
Previous article:ARM Linux: Implementation principle of switching from usr mode to svc mode
Next article:Causes and treatment measures of ARM abnormal interruption
- Popular Resources
- Popular amplifiers
- 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
- When using an oscilloscope probe to measure the AC waveform between the two ends of a component, how should the probe ground wire be connected?
- 【ST NUCLEO-H743ZI Review】(2)Ethernet Test
- TI.com Online Purchasing Special (Smart Building) has a limited time offer, with discounts as low as 30%!
- DLP Lightcrafter 4500 EVM FAQ Summary
- NRF24L01 module usage
- AD 20.2.4 x64
- Design and implementation of high-speed SRIO interface of TMS320C6455
- When the frequency of PWM square wave signal is low, voltage overshoot and ringing will occur?
- 【bk7231N】Exploration of Tuya products in SPI and other communication aspects
- Commonly used algorithms for drones - Kalman filter (VII)