function pointer, function pointer array, function return value as function pointer

Source: Internet
Author: User

The name of the function is the first address of the function: The function pointer is defined, int (*p) (int) p is the function pointer variable name, int is the function's return value type int, and (int) is the parameter type of the function int type, note: Because of the priority so to use (*p), Otherwise, p is combined with the back () to int*p (int), which means that the return value of the P (int) function is int*Note: In the main2 () function Int (*p[]) (int, int)is a one-dimensional array, the following is wrong,
#include <stdio.h>#include<stdlib.h>intAddintAintb) {    returnA +b;}intSubintAintb) {    returnAb;}intMultiplition (intAintb) {    returnA *b;}intDiveintAintb) {    returnAb;}voidmain1 () {int(*p (int,int))(int,int);//The return value type of the P (int,int) function is int (*) (int, int) (function pointer type)System ("Pause");}voidmain2 () {intA =Ten;intb = -; //define a two-dimensional function pointer array    int(*p[]) (int,int) = {Add, sub, multiplition, dive};//" " can be written in the length of the array, or can not write, and ordinary array of the same//drop function with function pointerprintf"%d", p[1] (A, b)); //The function is called by traversing the function pointer and iterating through the array.     for(inti =0; I <4; i++) {printf ("%3d", P[i] (A, b)); }    //because * (p+i) and p[i] are equivalent, so:     for(int(**PS) (int,int) = P; PS < p +6; ps++) {printf ("%3d", (*PS)    (A, b)); }    //Int (*p) (int, int)//Int (*p[]) (int, int)//Int (**p) (int, int)System ("Pause");}voidmain33 () {//int a[3]={1,2,3}//Int (*p[]) (Int,int) ={add, Sub, Multiplition, dive}//A and P are constants, so they cannot be a=a,p=p//int *pa= (int[]) {by-means}; This is the memory on the stack.//Int (**pt) (int, int) Two-level function pointer, storing the function pointer array name//function pointer array type int (*[]) (int,int)    int(**PP) (int,int) = (int(*[]) (int,int) {Add, Sub, multiplition, dive};  for(inti =0; I <4; i++)    {        //both of these methods areprintf"%3d", Pp[i] ( -,Ten)); //printf ("%d\n", (* (pp + i) ));} system ("Pause");}voidmain44 () {//open up memory on the heap    int(**PP) (int,int) =malloc(4*sizeof(int(*) (int,int))); //assigns a value to each array element, respectively;*PP =add; * (pp +1) =Sub; * (pp +2) =multiplition; * (pp +3) =Dive; //called separately     for(inti =0; I <4; i++) {printf ("%3d", (* (pp + i)) ( -, -)); } System ("Pause");}//normal pointers, arrays and function pointers, array of function pointers//int *p int (*p) (int, int)//int*p[4] Int (*p[]) (int, int)//int **p int (**p) (int, int)//typedef simplifies writing//define variables, type + variable names first//typedef type + variable name//the variable name is the alias of the type//int X;typedefintX; //int y[10];typedefinty[Ten];//Int (*z) (int, int);typedefint(*z) (int,int);//Int (*w[]) (int, int);typedefint(*w[]) (int,int);//Int (**pp) (int, int)typedefint(**PP) (int,int); voidMain55 () {X x1;    Y Y1;    Z Z1; W W1={Add, Sub,}; PP PP1=(W) {Add, sub}; printf ("%3d",sizeof(W1)); System ("Pause");}//Int (* * x (int (*z) (int, int), int,double)) (int);//x (int (*z) (int, int), int,double)//the parameter of the X () function is int (*z) (int, int), int,double//Int (*z) (int, int) is a function pointer//Int (* *) (int) is a function return value type//the return value of x two-level function pointer, parameter, has a function pointer, int,double,//x is a function//z function pointer//Int (*p) [5];//Pointer to array//int *p[5];//array, each element is a pointer//Int (*p[5]) (int, int);//array of function pointers//Int ((* (P)) [5]) (int, int);//pointer to array of function pointersvoidMain () {int(*p[5])(int,int); int(* (*P) [5])(int,int);//pointer to an array of function pointers,//*p is a pointer, int (*[5]) (int, int) is an array of function pointers to understand,System ("Pause");}

function pointer, function pointer array, function return value as function pointer

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.