function pointers, array of function pointers

Source: Internet
Author: User
Tags function prototype

Reference: Baidu Encyclopedia | function pointer | entry.

Pointer _ function, do not say. Oneself feel is so-easy. [Declaration format: ReturnType *function (arguments);]

The focus is on the function pointer, and an array of function pointers that suddenly pops up; (specifically, I'm used to writing code first, then commenting; case A: The code is left, the comment is to the right.) Case B: The code is on, the comment is under. )

function pointer , my image of the description function <-pointer , (the text all have a problem please correct me, thank you). [Declaration format: ReturnType (*pointer) (arguments);]

Example: int fun (int x);//declaring a function;

int *f (int x) =fun; Declares a function pointer and points to the first address of the fun function;

The same effect: f=&fun; F is a function pointer already defined, using the & symbol Get function's first address, but & is optional, in order to clear this can be written;

Note: 1. When assigning a value, the function fun does not have parentheses and arguments,

2. The function fun represents the first address of the function, so after the assignment, the pointer points to the first address of the fun (x).

~ 3. the formal parameters of the function brackets are optional, depending on the case .

Example: This program illustrates the method by which a function pointer invokes a function:

#include <stdio.h>int max (int x,int y) {return (x>y? x:y);} int main () {    int (*ptr) (int, int);  Declares a function pointer *pst, which will point to the first address of a functor with a return value of int and a parameter of two int;    int A, B, C;    ptr = max;   Assigns the first address of Max to the function pointer PST;    scanf ("%d%d", &a, &b);    c = (*ptr) (A, b);  Use the PST call to the MAX function to assign the return value to the variable C    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, that is, the entry address of Max () is assigned to PTR, you can call the function with PTR, in fact, both PTR and Max point to the same ingress address, and the difference is that PTR is a pointer variable. Not as dead as the function name, 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 that point to functions do not have the + + and--operations, so be careful.

However, in some compilers this is not possible. The supplement to this example is as follows: 1. Define the function pointer type:typedef int (*FUN_PTR) (int,int);2. Declare variables, assign values:fun_ptr Max_func=max;In other words, the function assigned to the function pointer should be consistent with the function prototype that the function pointer refers to. Example2:
1#include <stdio.h>2 3 voidFilefunc () {4printf"filefunc\n");5 }6 voidEditfunc () {7printf"editfunc\n");8 }9 Ten intMain () { Onetypedefvoid(*FUNCP)   ();  A      -FUNCP pfun=Filefunc; - Pfun (); the      -pfun=Editfunc; - Pfun (); -}

Next, is to understand pointer FunctionsAnd function PointersThe difference: pointer function: The return value is a function of a pointer. The actual nature is still a function. The function has a return type in addition to void, and the function pointer means that the return type is a pointer to a type. Definition Format: Return type identifier * Function name (formal parameter table) {/*...*/} The return type can be any base type and composite type. Functions that return pointers are very versatile. Thing actually, every function, even if it does not carry a pointer that returns a type, has an entry address of its own, the address 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" to a Example3:
1#include <iostream>2 using namespacestd;3 intMain () {4     float*find (float(*p) [4],INTM);//The results of the four courses of the students who queried the number m5     floatscore[][4]={{ -,Wuyi, the, -},{ -, -, +, the},{ the, About, the, the}};//define an array of scores, the first dimension can be a variable6     float*pf=null;//when defining a pointer, be sure to initialize it7     inti,m;8 9cout<<"Please enter the number of the student you would like to inquire about:";TenCin>>m; OnePf=find (SCORE,M);//returns a pointer to a one-dimensional array, pointing to a student's score A  -      for(i=0;i<4; i++) -cout<<* (pf+i) <<""; the      -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 A     returnPF; at}
/** student number from number No. 0, function find () is defined as a pointer function. The parameter pointer is a pointer to a one-dimensional array containing 4 elements;
* PF is a pointer variable that points to the float variable. The Find () function is called in the main () function to pass the first address of the score array to pointer.
*/

Finally , it is an array of function pointers :

Defined..... I also understand, understand and write again!

function pointers, array of 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.