Big talk function pointer and pointer function, big talk function pointer

Source: Internet
Author: User

Big talk function pointer and pointer function, big talk function pointer

I. Cause

(1) A pointer function is a function with a pointer. The return type of a function is a pointer of a certain type, which is a common function type and the return value is a pointer.

(2)A function pointer is a pointer variable pointing to a function. It is essentially a pointer variable and a pointer pointing to the first address of a function (possibly a code area). As we all know, the array name is the constant pointer pointing to the first element of the array (for details, see array collection).Similarly, for a function, the function name is also a constant pointer to the first instruction of the function. After the program is compiled, the compiler allocates a first address for each function, that is, the address of the first instruction of the function. In general, we can use a pointer to save this address, and this pointer is a function pointer, which can be seen as its alias pointing to the function, so we can use this pointer to call this function.

(3) pointer application is the essence of C language programming, and callback is an advanced application of function pointers in C language. In short,A callback function is a function called by a function pointer. If you pass the function pointer (the function entry address) to another function, when this function pointer is used to call the function to which it points, we will say that this function is a callback function.

(4) we are very familiar with and often use sort (begin, end) Functions and sort (begin, end, cmp ); the name of the cmp main function (that is, the first address of the function is represented like the array name ):

bool cmp(void const *a,void const *b){return *((int *)a) > *((int *)b);}

Note: For forced type conversion, the parameter of the comparison function must be declared as void * to match the prototype of the search function, and then be forcibly converted to the (int *) type for integer comparison.Then, a pointer pointing to this function is passed as a parameter to the lookup function, and The lookup function calls this comparison function to execute the comparison. Using this method, any type of value can be compared.

(5)Note that pointer functions and function pointers indicate different methods. Do not confuse them. The simplest way to identify a function is to check whether the pointer * in front of the function name is included in parentheses (). If it is included, it is a function pointer. Otherwise, it is a pointer function.

Ii. Detailed Analysis

(1) A pointer function is a function with a pointer. A function returns a pointer of a certain type.

Type identifier * function name (parameter table)

Int * f (x, y );

First, it is a function, but the return value of this function is an address value.Function return values must be accepted by pointer variables of the same type. That is to say, pointer functions must have function return values.In addition, in the main function, the function return value must be assigned to pointer variables of the same type. For example:

Float * fun ();

Float * p;

P = fun ();

(2)A function pointer is a pointer variable pointing to a function.

Int (* f) (int x);/* declare a function pointer */

F = func;/* assign the first address of the func function to the pointer f */
In fact, it cannot be called the function name. It should be called the variable name of the pointer.This special Pointer Points to a function that returns an integer value..The pointer statement is the same as the statement pointing to the function.
Parentheses outside the pointer and pointer operators change the default operator priority. Without parentheses, it becomes a prototype declaration of a function that returns an integer pointer. For example:
Void (* fptr )();
Assign the function address to the function pointer in the following two forms:
Fptr = & Function;
 Fptr = Function;// This method is recommended.
It is not necessary to use the address operator, because a Function Identifier represents its address. If it is a function call, it must also contain a parameter table enclosed in parentheses.
You can use the following two methods to call a function through a pointer:
X = (* fptr )();
X = fptr (); // This method is recommended

(3) obfuscation:

Function pointer definition:

Type (* func) (type &, type &) or type (* func) (type, type)

This statement declares a pointer func that points to a function with two type parameters and returns a value of type. (The p.s. type can be regarded as the C ++ type, such as int or floast ).

A: A pointer to A function must ensure that the function is defined and allocated with memory. Otherwise, it will point to an empty address!

B: pay special attention to the position of the first bracket. If we do not write parentheses, as follows:

Type * func (type, type)

This is not a pointer to a function. Instead, it declares a function.ReturnA type pointer

With the pointer variable pointing to the function, the function can be called using the pointer variable, just like using the pointer variable to reference other types of variables. These concepts are basically the same.The function pointer has two purposes: calling a function and making a function parameter.

( 4 ) Note that pointer functions and function pointers indicate different methods. Do not confuse them. The simplest way to identify is to look at the pointer before the function name. * Is the number enclosed by parentheses ()? If it is included, it is a function pointer; otherwise, it is a pointer function.

Iii. Advanced Application of function pointers-as a function parameter

(1) In the following program, three function pointers of different types are given, respectively marked and distinguished with (1) (2) (3). They are divided into declaration and implementation parts, call part.

(2) Code

# Include <iostream> using namespace std; void my_swap (int &, int &); // (4) Declaration of common functions int my_min (int, int ); // (5) Declaration of common functions void my_input (void (* func) (int &, int &); // (1) void my_input (int (* func) (int, int); // (2) void my_print (int, int (* func) (int, int); // (3) Declaration of int main () {cout <"please input two numbers: "<endl; my_input (my_swap); // (1) calls with function pointer parameters. The function name is the first address in the function code area: my_input (m Y_min); // (2) call with the function pointer parameter. The function name is the first address in the function code area: int a, B; cin> a> B; my_print (a, B, my_min); // (3) calls with function pointer parameters. The function name is the first address in the function code area. return 0 ;} void my_input (void (* func) (int &, int &) // (1) implementation with function pointer parameters {int a, B; cin> a> B; func (a, B); // The first address of the function code area pointed to by the pointer <a <",,, "<B <endl;} void my_input (int (* func) (int, int) // (2) implement with function pointer parameter {int a, B; cin> a> B; int mi = func (a, B); // The first address of the function code area pointed to by the pointer cout <"a, B: "<Mi <endl;} void my_print (int a, int B, int (* func) (int, int) // (3) implementation with function pointer parameters {int mi = func (a, B); // The first address of the function code area pointed to by the pointer cout <a <",, "<B <", "<mi <endl;} int my_min (int a, int B) // (5) {return a <B? A: B;} void my_swap (int & a, int & B) // (4) {a ^ = B; B ^ = a; a ^ = B ;}


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.