Function pointer, function pointer, a little ignorant laps? Can you distinguish it

Pointer functions and function pointers in the end how to distinguish?

1 Introduction

Arrays, pointers, in a different order combination of two words, its meaning is different.

Similarly, the function pointer binding order of these two different words which are different meaning, i.e. different meaning and function pointer function pointers.

 

2, the function pointer

The nature of the target function is a function, the return value is a pointer. Examples are as follows:

int *pfun(int, int);

As the "  " lower priority than "()" takes priority, and thus pfun first "()" in conjunction with the back, which means, pfun is a function. That is: int * (pfun (int, int));

Followed by the previous "*" binding, indicating that the return value of this function is a pointer. Since there is a front int, that is, pfun return value is a pointer to an integer function.

Example procedure pointer to the function as follows:

Program results are as follows:

The main function, the first address and the array length of an array pointer passed as an argument pfun function, the return value of the function pointer (i.e., a pointer to an array) is assigned shaping pointer p. Finally, pointer p to traverse the array elements and the printed output.

 

3, function pointer

Function pointer which is essentially a pointer variable, the variable is a pointer to a function. C program at compile time, each entry has an address of a function, the entry address is the function pointer points to the address. Function pointer Example:

func is a function name, then the func and & func have said that the entry address of the function . Similarly, you can use the calling function: Mode 1: FUNC () , can be used two ways: (* Fun) () . Both calls are equivalent, but mostly we usually call the method by way of both a habit.

As for why & func func with the same meaning:

For the function func, the name of the function is a constant function of the code region, it takes address (& func) address of the function code region can be obtained, while, fun itself can also function as the address code space. Therefore, the function takes its name, and its meaning is the same address.

Function pointer exemplary procedure is as follows:

Program results are as follows:

These are the simple distinction on the function pointer with function pointers. Where the function pointer widely used in embedded software development, which is commonly used for two purposes: to call the function and do function parameters.

For those who love programming, the answer together with a group of small learning partner is very important! I have started to learn a programming zero-based exchange club ( group ), as well as learning video files, are welcome beginners and advanced in little friends!

Published 520 original articles · won praise 132 · views 80000 +

Guess you like

Origin blog.csdn.net/HUYA69/article/details/105247165