C function pointers

Source: Internet
Author: User

     A function pointer is a pointer variable that points to a function. Thus the function pointer itself should first be a pointer variable, except that the pointer variable points to the function. This is just like using pointer variables to point to integer variables, character types, and arrays, and here is a pointer to a function. As mentioned earlier, C at compile time, each function has an entry address, which is the address pointed to by the function pointer. Once you have a pointer variable to a function, you can call the function with that pointer variable, just as you can refer to other types of variables with pointer variables, which are broadly consistent in these concepts. A function pointer has two uses: calling a function and making a function argument.
1 method function pointers are declared by: return value type (*pointer variable name) ([formal parameter list]); Note 1: "Return value type" describes the return type of the function, the parentheses in the "(pointer variable name)" cannot be saved, and the parentheses change the precedence of the operator. Omitting the whole becomes a function description, indicating that a function that returns a data type is a pointer, followed by a parameter list that represents a list of arguments with the function that the pointer variable points to. For example:intFuncintx);/*declaring a function*/int(*f) (intx);/*declaring a function pointer*/F=func;/*assigns the first address of the Func function to the pointer F*/Alternatively, assign the function address to the function pointer using the following method: F= &func; The function func has no parentheses and no arguments, since Func represents the first address of the function, so after the assignment, the pointer F points to the first address of the code of the function func (x). Note 2: Parameters in the function brackets are optional, depending on the situation. The following procedure describes how a function pointer invokes a function: Example one,
#include <stdio.h>intMaxintXintY) {return(x>y?)x:y);}intMain () {int(*PTR) (int,int); intA, B, C; PTR=Max; scanf ("%d%d", &a, &b); C= (*ptr)    (A, B); printf ("a=%d, b=%d, max=%d", A, b, c); return 0;}
 ptr is a pointer variable to a function, so you can assign the function max () to PTR as the value of PTR, which is to assign the entry address of Max () to PTR, which can then be invoked with PTR, in fact both PTR and Max point to the same ingress address. The difference is that PTR is a pointer variable, not as dead as the function name, and it can point to any function and see what you want to do. The address of which function is assigned to it in the program, and it points to which function. It is then called with a pointer variable, so you can point to different functions successively. Note, however, that pointer variables pointing to functions do not have + + and-- operation, take care. However, in some compilers this is not possible. The supplement to this example is as follows. This should be the case:  1  . Defines the function pointer type: typedef  Span style= "color: #0000ff;" >int  (*fun_ptr) (int , int  ); 2   =max; in other words, The function assigned to the function pointer should be consistent with the function prototype that the function pointer refers to. Example two,  
 #include <stdio.h>void   Filefunc () { printf (  filefunc\n   "  void   Editfunc () {printf ( "  editfunc\n   );}  void   Main () {typedef  void  (*FUNCP) ();    FUNCP pfun  =FILEFUNC;    Pfun ();    Pfun  =EDITFUNC; Pfun ();}  
2 Comparison of differences between pointer functions and function pointers: 1 , these two concepts are short, the pointer function refers to the function that the return value is a pointer, that is, the essence is a function. We know that a function has a return type (or no value if it does not return a value), except that the pointer function return type is a pointer to a type. Its definition format is as follows: return type identifier * function name (formal parameter table) {function Body} The return type can be any base type and composite type. Functions that return pointers are very versatile. In fact, every function, even if it does not carry a pointer that returns a certain type, itself has an entry address that is equivalent to a pointer. For example, the function returns an integer value, which is actually equivalent to returning the value of a pointer variable, but the variable is the function itself, and the entire function is equivalent to a "variable". For example, the following example returns a pointer function:
//A pointer function is a function in which the return value is a pointer, which is essentially a function:#include <iostream>using namespacestd;intMain () {float*find (float(*p) [4],INTM);//The results of the four courses of the students who queried the number m    floatscore[][4]={{ -,Wuyi, the, -},{ -, -, +, the},            { the, About, the, the}};//define an array of scores, the first dimension can be a variable    float*pf=null;//when defining a pointer, be sure to initialize it    inti,m; cout<<"Please enter the number of the student you would like to inquire about:"; CIN>>m; PF=find (SCORE,M);//returns a pointer to a one-dimensional array, pointing to a student's score     for(i=0;i<4; i++) cout<<* (pf+i) <<""; cout<<Endl; return 0;}float*find (float(*p) [4],intm) {float*pf=NULL; PF=* (P+M);//p is a pointer to a two-dimensional array, plus a pointer to a one-dimensional array    returnPF;} 
Student number from number No. 0, function find () is defined as a pointer function whose parameter pointer is a pointer to a one-dimensional array containing 4 elements. PF is a pointer variable that points to a floating-point variable. The Find () function is called in the main () function to pass the first address of the score array to pointer.
The 3 pointer array defines a method for defining a function pointer array, in two ways: one is the standard method, and the other is the deception method. The first, standard method: {parse: An array of function pointers is an array whose elements are function pointers. So that is, this data structure is an array, and its element is a pointer to the function's entry address. According to the analysis: The first explanation is an array: array name [] Second, the data type pointer of its element:* array name []. Again, to make sure that each of these array elements is a pointer to the function entry address: The function returns a value type (* array name []). Please note that Why do you want to extend the "* array name []" in parentheses? Because the precedence of parentheses and array specifiers is the same, if you do not use parentheses to extend the expression of pointer arrays, according to the binding direction of the parentheses and brackets, what is the array name [] () described? is an array of functions whose elements return a value type as a pointer. Do you have such an array of functions? I don't know. So it must be enclosed to ensure that each element of the array is a pointer. Second, Deception: Although the function is not a variable, it still has its physical address in memory, which can be assigned to pointer variables. The way to get the address of a function is by using a function name without parentheses and arguments. The function name corresponds to a pointer to its function entry constant. So since the function name is a pointer constant, it can be handled in some appropriate ways, such as coercion of type conversions. Then we can put this address in an array of shaping pointers, and then call it as a function pointer. Complete Example:
#include <stdio.h>intADD1 (intA1,intB1);intADD2 (intA2,intB2); intMainvoid){    intNUMA1 =1, NUMB1 =2; intNUMA2 =2, NUMB2 =3; int(*op[2])(intAintb); op[0] =add1; op[1] =add2; printf ("%d%d\n", op[0] (NUMA1, NUMB1), op[1] (NUMA2, NUMB2));} intADD1 (intA1,intB1) {    returnA1 +B1;} intADD2 (intA2,intB2) {    returnA2 +B2;}
  1   1  void  (*array[]) (void ) ={stop,run,jump}; Essentially, the array of function pointers is still an array, so the definition of arrays is similar, because it is statically assigned, the numbers inside [] can be omitted. There are three members of this function pointer array.  1   array[ 1 ] (); //  Execute the Run function  2   
void (*ifunc) (void); // this type of function pointer points to a function that has no parameters and no return value. funcint array[]; // defines an array of function pointers with each member being a function pointer of type Intfun array[]=int_timer0; // assign a value to it array[] (); // a function that calls the 11th member of a function pointer array

C function pointers

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.