This article refer to http://www.prglab.com/cms/pages/c-tutorial/advanced-data/pointers.php
http://blog.csdn.net/ameyume/article/details/8220832
1. Function pointers
A function pointer is a pointer to a function , not a normal data type or class object. It is defined as follows:
int (*func) (intint b); // declaring function Pointers
The maximum function pointer is to pass one function as an argument to another function. The declaration of a function pointer is similar to declaring a normal function prototype, except that the function name of the function pointer needs to be enclosed in parentheses and an asterisk asterisk (*) is added before the function name. This means that the function name of the function pointer is a pointer , and the function pointer func above is defined as a function that points to a return value of integral type, has two parameters and two parameters are integral types.
The following is a function pointer usage that asks for a two number and a difference:
//Pointer to functions#include <iostream.h>using namespacestd;//ask for two numbers andintAddition (intAintb) {return(A +b); }//ask for a difference of two numbersintSubtraction (intAintb) {return(A-b); }int(*minus) (int,int) =subtraction;intOperation (intXintYint(*functocall) (int,int)) { intG; G= (*Functocall) (x, y); return(g);} int(*func) (int,int);//declaring function PointersintMain () {intM,n; /*call func=addition in the form of a function pointer; int res= (*FUNC) (7,5); Func=subtraction; int res1= (*FUNC) (20,res); */m= Operation (7,5, addition);//calling the Sum functionn = operation ( -, M, minus);//Call the Find difference functioncout <<N; System ("Pause"); return 0; }
2. Pointer functions
A pointer function is a function with a pointer, which is essentially a function , except that the return type is a pointer to a type;
It is defined as follows:
Type descriptor (*) function name (parameter list); int * func (int x,int y);
As can be seen from the above definition, the pointer function is a function that returns a value of address type. It is important to note that the return value of a function must be accepted by a pointer variable of the same type (that is, the pointer function must have a return value). The use of the return value is as follows:
Char *fun (); // declares a pointer function that returns a type of char Char *p; // defines a pointer variable of type char, which is used to receive the return value of fun () p=fun ();
Examples of use of pointer functions:
#include <iostream>using namespacestd;int* GetDate (intWeekintDay);//declaring pointer functionsintMain () {intWeek,day; Do{cout<<"Please enter week (1--5) Day (1--7)"<<Endl; CIN>>week>>Day ; } while(week<1|| Week>5|| day<1|| Day>7) cout<<*getdate (Week,day);//to get the value of an element under that addresscout<<Endl; System ("Pause"); return 0;} int* GetDate (intWeekintDay ) { Static intcalendar[5][7]= { {1,2,3,4,5,6,7}, {8,9,Ten, One, A, -, -}, { the, -, -, -, +, -, +}, { A, at, -, -, -, -, -}, { in, -, to,-1} }; return&calendar[week-1][day-1];//returns the address that points to an element of the calendar}
The following examples refer to http://blog.csdn.net/ameyume/article/details/8220832 to illustrate the conversion of memory addresses during the use of pointer functions
#include <iostream>using namespacestd;int*f (intAintb);//declaring pointer functionsintMain () {cout<<"------------------------------start\n"; int*P1 =NULL; cout<<"The memeory address of p1 = 0x"<< P1 <<Endl; P1= f (1,2); cout<<"The memeory address of p1 = 0x"<< P1 <<Endl; cout<<"*P1 ="<< *P1 <<Endl; cout<<"------------------------------end\n"; System ("Pause"); return 0;}/** pointer function definition * Return value is pointer type int **/int*f (intAintb) {int*p = (int*) malloc (sizeof(int)); cout<<"The memeory address of p = 0x"<<p <<Endl; Memset (P,0,sizeof(int)); *p = A +b; cout<<"*p ="<< *p <<Endl; returnp;}
The results of the operation are as follows:
C + + function pointers and pointer functions