The differences between function pointers and pointer functions and how to apply them.

Source: Internet
Author: User

I have always forgotten the difference between the two. Every time I use them, I will pick them up again. The parsing here is simple, so I will add them to my favorites !!

Source: http://zhidao.baidu.com/question/264795890.html

1. function pointer variable
In C language, a function always occupies a consecutive memory zone, and the function name is the first address of the memory zone occupied by the function. We can assign the first address (or entry address) of the function to a pointer variable so that the pointer variable points to the function. Then, the pointer variable can be used to locate and call this function. We call this pointer variable pointing to a function "function pointer variable ".
Function pointer variables are defined as follows:
Type specifier (* pointer variable name )();
The "type specifier" indicates the type of the return value of the specified function. "(* Pointer variable name)" indicates that the variable after "*" is the defined pointer variable. The final empty parentheses indicate that the pointer variable refers to a function.
For example:
INT (* PF )();
PF is a pointer variable pointing to the function entry. The return value (function value) of this function is an integer.
[Example] This example describes how to use a pointer to call a function.
Int max (int A, int B ){
If (A> B) return;
Else return B;
}
Main (){
Int max (int A, int B );
INT (* Pmax )();
Int x, y, z;
Pmax = max;
Printf ("input two numbers: \ n ");
Scanf ("% d", & X, & Y );
Z = (* Pmax) (x, y );
Printf ("maxmum = % d", Z );
}

The procedure for calling a function in the form of a function pointer variable is as follows:
1) define the function pointer variable first, for example, the second line of INT (* Pmax) () in the next program, and define Pmax as the function pointer variable.
2) Assign the entry address (function name) of the called function to the function pointer variable, for example, 11th rows of Pmax = max in the program;
3) call a function in the form of a function pointer variable, such as the program's 14th line z = (* Pmax) (x, y );
4) The general form of calling a function is:
(* Pointer variable name) (real parameter table)
Note the following when using function pointer variables:
A) function pointer variables cannot be used for arithmetic operations, which is different from array pointer variables. Adding or subtracting an integer from an array pointer variable can move the pointer to an array element behind or before it, and moving the function pointer is meaningless.
B) In a function call, the brackets on both sides of "(* pointer variable name)" are indispensable. * should not be considered as a value calculation, where * is only a symbol.

2 pointer Functions
As we mentioned earlier, the so-called function type refers to the type of the function return value. In C language, the return value of a function is a pointer (that is, an address). The function that returns the pointer value is called a pointer function.
The general form of defining pointer functions is:
Type specifier * function name (parameter table)
{
...... /* Function body */
}
"*" Is added before the function name, indicating that this is a pointer function, that is, the return value is a pointer. The type description indicates the Data Type pointed to by the returned pointer value.
For example:
Int * AP (int x, int y)
{
.../* Function body */
}
AP is a pointer function that returns the pointer value. It returns the pointer to an integer variable.
[Example] This program uses a pointer function to input 1 ~ An integer between 7 and the name of the week.
Main (){
Int I;
Char * day_name (int n );
Printf ("input day no: \ n ");
Scanf ("% d", & I );
If (I <0) Exit (1 );
Printf ("day no: % 2D --> % s \ n", I, day_name (I ));
}
Char * day_name (int n ){
Static char * name [] = {"illegal Day ",
"Monday ",
"Tuesday ",
"Wednesday ",
"Thursday ",
"Friday ",
"Saturday ",
"Sunday "};
Return (n <1 | n> 7 )? Name [0]: name [N]);
}

In this example, a pointer function day_name is defined, and its return value points to a string. This function defines a static pointer array name. The name array is initialized with eight strings, indicating the names of each week and error messages. The parameter n represents the integer corresponding to the name of the week. In the main function, the input integer I is used as the real parameter. In the printf statement, the day_name function is called and the I value is transmitted to the form parameter n. The Return Statement in the day_name function contains a condition expression. If n is greater than 7 or less than 1, the name [0] pointer is returned to the main function. An error occurs when the output of the main function returns the string "illegal day ". Otherwise, the week name corresponding to the output of the main function is returned. The first row in the main function is a condition statement. Its syntax is: if the input is negative (I <0), stop the program and exit the program. Exit is a library function. Exit (1) indicates to exit the program after an error occurs, and exit (0) indicates to exit normally.
Special attention should be paid to the difference between writing method and meaning between function pointer variables and pointer functions. For example, INT (* p) () and int * P () are two completely different quantities.
INT (* p) () is a variable description, indicating that p is a pointer variable pointing to the function entry. The return value of this function is an integer value, (* P) the parentheses on both sides.
Int * P () is not a description of variables, but a description of functions. P is a pointer-type function, and its return value is a pointer to an integer. * P has no parentheses on both sides. As a function description, it is best to write a formal parameter in brackets, so that it is easy to distinguish it from the variable description.
For pointer-type function definition, int * P () is only the first part of the function. Generally, the body part of the function should be included.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.