C. A new pointer: function pointer variable, pointer-Type Function

Source: Internet
Author: User

A glance is a bit vague; note the difference:

    1. function pointer: the type of the function return value..

   Pointer function: the return value is a pointer (address)

2. Is int (* p) () the same as int * p? Why?

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. -->Function pointer

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 formal parameters in brackets.-->Pointer Functions

     

I. function pointers

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.

Example: int (* p )();

P indicates a pointer variable pointing to the function entry. The return value (function value) of the function is an integer.

Eg:

  

Main () {int SelectMax (int a, int B); // function declaration int (* pmax) (); // The general form of function pointer variable definition is: type specifiers (* pointer variable name) (); int x, y, z; pmax = SelectMax; // pointer value assignment. Do not use parentheses printf ("enter two digits: \ n "); scanf (" % d ", & x, & y); // give. B value assignment z = (* pmax) (x, y); // call method printf ("maximum value: % d \ n minimum value: % d \ n", x, y);} int SelectMax (int a, int B) {if (a> B) return a; else return B ;}

Result:

Explanation:

1. Define the function pointer variable first, for example, the second line of int (* pmax) () in the next program. 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, the 11th rows of pmax = SelectMax in the program;

3. Call a function in the form of a function pointer variable, for example, the program's 14th line z = (* pmax) (x, y );

The common form of calling a function is:
(* Pointer variable name) (real parameter table)

4. Pay attention to the following two points when using function pointer variables:

1. 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 after or before it, and moving the function pointer is meaningless.

2. In function calls, the brackets on both sides of "(* pointer variable name)" are indispensable. * should not be understood as a value calculation, where * is just a symbol.

 

 

Ii. pointer Functions

Function type refers to functionReturn ValueIsValue. The C language allows a functionThe returned value is a pointer (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.

 

Eg:

  

Main () {int I; char * day_name (int n); printf ("input days: \ n"); scanf ("% d", & I ); if (I <0) exit (1); printf ("today is: % 2d --> % s \ n", I, day_name (I ));} har * day_name (int I) {static char * name [8] = {"Invalid Number", "Monday", "Tuesday", "Wednesday", "Thursday ", "Friday", "Saturday", "Sunday"}; return (I <1 | I> 7? Name [0]: name [I]);}

Result:

 

Differences between function pointers and pointer functions:

    Function pointer int (* p )()    

The pointer function int * p () usually has the function body int * p (){......}

 

 

 

Author: PEPE
Source: http://pepe.cnblogs.com/
The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.

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.