C language: pointer functions and function pointers

Opener: Take both concepts literally

Function pointer: is a pointer variable that points to a function (that is, itself is a kind of pointer)

The definition format is as follows:

data type identifier (*pointer variable name) (parameter)
void (*pf)(char *);//pf is a pointer to a function, the type of the function's return value in void, and char* is the type of function parameters

Pointer function: The essence is a function, but the return value is a pointer of a certain type

The definition format is as follows:

Return type identifier * Returned pointer name (parameter)
void * pf(char *);//pf is a function that returns a pointer

===============================================================

Focus on understanding function pointers : Just as pointer variables can be used to point to integer variables, character types, and arrays, here is a pointer to a function. As mentioned above, when C compiles, each function has an entry address, and the entry address is the function The address pointed to by the pointer. After you have a pointer variable to a function, you can use the pointer variable to call the function, just as you can use the pointer variable to refer to other types of variables, which are conceptually consistent.

In general, if you want to declare a data pointer, you must specify the type of data it points to. Therefore, when declaring a function pointer, we also need to specify the type of the function it points to. To specify the type of the function, we must specify the return type of the function and the parameter types of the function. For example, for the following ToUpper function: void ToUpper (char *); the type of the function ToUpper is: "a function with a parameter of type char* and a return type of void", then if you want to declare a pointer variable pf pointing to this function, you can do this Do: void (*pf) (char *); That is, directly use (*pf) to replace the function name (due to the problem of operation priority, the parentheses must be brought)

Note: When pointing a function pointer to a function, be sure to match the types

Function pointers serve two purposes: to call a function and to be an argument to a function

① call function

Just as we can use data pointers to access data, we can also use function pointers to access (call) functions, there are two logically inconsistent syntaxes for calling functions (ANSI C accepts both as equivalents) )

For example the following code:

void ToUpper (char *);
void ToLower(char *);
void (*pf)(char *);
char name[] = "Linden";
pf = ToUpper;
(*pf)(name);//Apply ToUpper to name, the first syntax
pf = ToLower;
pf(name);//Apply ToLower to name, the second syntax

The above two calling methods are equivalent


②Make the parameters of the function

Just as one of the most common uses of data pointers is as parameters to functions, the most common use of function pointers is as parameters to functions. E.g:

void show (void (*fp)(char *),char *str);//void (*fp)(char *) is a function pointer here as a parameter

Given the preceding declaration, the function can be called with:

show (ToLower,name);//show() uses the ToLower() function: fp = ToLower
show (pf,name);//show() uses the function pointed to by pf: fp = pf

How to use the passed function pointer when show function? It calls the function using the syntax fp( ) or (*fp)( ) described above:

void show (void (*fp)(char *),char *str)
{
	(*fp)(str);//Apply the selected function to str, or the syntax fp(str); you can also, as explained above, the two are equivalent
	puts(str);//Display the result
}


The use of pointer functions is the same as that of general functions, but pay attention to the return value. For a function that returns a pointer, the address of an auto-type local variable cannot be returned, but the address of a static-type variable can be returned.

This is because the life cycle of the auto type variable is very short. When the function returns, the memory space of the auto type variable will be released. If the return value is an auto type variable, the return pointer will be invalid and become a wild pointer. The memory space occupied by static type variables will not be released due to the return of the function, and there will be no wild pointer problem.

So write pointer functions to pay attention to the return value. The general principle is: the memory space corresponding to the returned pointer will not be released when the function returns. Commonly used return pointers are as follows:

(1) The first address of the dynamically allocated memory space (implemented by malloc, etc.) in the function;

(2) The first address of the variable corresponding to a static variable (static) or a global variable;

(3) The effective address of the actual parameter obtained through the pointer parameter.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325933600&siteId=291194637