Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to define a pointer to a function?
How to define a pointer to a function?
First of all, the method of defining a function pointer is as follows:

Return value type (* pointer variable name) ([parameter list]);

1、int func(int x); /* Declare function */

2、int(* f)(int x); /* Declare a function pointer */

3. f = func/* Assign the first address of the func function to the pointer f */

Second, the usage of function pointer:

Application of function pointer: (* pointer variable name) (parameter list)

Such as int c=(*ptr)(a, b); /* Use of function pointers */

In C language, the function itself is not a variable, but you can define a pointer to the function, also called a function pointer, which points to the entry address of the function. This type of pointer can be assigned, stored in an array, passed to a function, returned as a function, and so on. ?

Extended data:

The difference between pointer function and function pointer:

1, both of which are abbreviations. Pointer function refers to a function whose return value is a pointer, that is, it is essentially a function. We know that all functions have a return type (no return value is no value), but the return type of pointer function is some kind of pointer.

2. The function of returning a pointer is widely used. In fact, each function has its own entry address, which is equivalent to a pointer, even if it does not return a pointer of a certain type. For example, if a function returns an integer value, it is actually equivalent to returning the value of a pointer variable, but the variable at this time is only the function itself, and the whole function is equivalent to a "variable".

Baidu encyclopedia-pointer

Baidu Encyclopedia-Function Pointer