Pointers to functions basic grammar of C language

Pointer is the essence of C language for beginners, the pointer is a C language grammar learning more difficult knowledge, which is inside the pointer to a function is not easy to understand.

Here to tell everyone how to study and understand the C language and programming methods pointer to a function and use examples.

Note: This is a basic grammar contents on the C programming language, C language bypass the Great God.

basic concept

First of all, the first pointer to a function not to think too hard, and it is normal pointer difference is not too large, but the definition of form differ.

For example, a pointer for ordinary shaping, the form is defined as follows:

int * p;

In the definition, the name is a pointer variable p, the symbol "*" illustrates p is a pointer, int pointer illustrate the integer format.

So, if we define a pointer to a function, the variable name is assumed as p, such that it points to such a function, this shaping function requires two parameters, its return value shaping parameters are defined as follows:

int (* p) (int, int);

This definition break it down, where, p is the name of the variable, the symbol "*" illustrates a pointer p is, since this is a pointer to a function, it must reflect the input and output parameters of the function information is defined, then the foremost int refers to the function return value of type int, behind (int, int) defines the shaping function requires two input parameters. In addition, it must be "*" and "p" must be written in parentheses (* p) form, otherwise, due to the brackets priority higher than the priority of *, without the brackets, then it becomes another meaning.

Contrast this with understanding, it seems pointer to a function pointer with ordinary difference is not too large.

Examples pointer programming functions

By use of the following example demonstrates a pointer pointing function.

Examples of this feature is that a one-dimensional array for input, define three functions findMax, findMin and getAvg, respectively, to achieve the maximum value of the lookup array, and calculates the average of the minimum value of the array, the input and output of these three functions parameters are identical. Fun define a function, the function parameters, it is necessary as a pointer variable parameter, the pointer can point above three functions. In the main program, the function call fun, different incoming p value is achieved according to the processing functions for the different input one-dimensional array.

Look under the following sections of the code that implements it.

1, findMax, findMin code implementation and getAvg

These three functions of one-dimensional array x, respectively, for seeking process maximum, minimum and average values, and returns its result. C language code as follows:

These three functions is relatively simple, exactly the same function prototype, the input parameter is a pointer pointing to double the number of elements x of x and n, the output parameter is the return value is a value of type double.

2, the function code is implemented fun

The function of three input parameters, the number of elements n for the first two points x and x double pointer, the third pointer as a function of the type, the pointer can point above three functions. C language code as follows:

Then, in the main program can call this function, simply enter a different p values, can be different processing operation on the input one-dimensional array.

3, the main test code

Main test code language C as follows:

43-44 lines define one-dimensional arrays of x and store the results of a data processing elements comprises five variable max, min and avg.

Lines 46-48, fun function call, a function findMax, findMin getAvg and passed to the function name as a parameter, and outputs the result of the processing operation on the console.

FIG its operation is shown as follows:

Seen, in this example, we have been successfully used as a pointer to a function parameter, and to give the correct operating results.

to sum up

Pointer to a function, which is defined in the form of understanding and grasp the basic usage not seem to be too difficult.

Portal: C programming language: hands-on examples to follow me function pointer basis points of grammar 

https://baijiahao.baidu.com/s?id=1616897994801163285&wfr=spider&for=pc

Guess you like

Origin www.cnblogs.com/tongongV/p/10989923.html