"C-language" functions, function pointers, arrays of pointers, array pointers.

Source: Internet
Author: User

First define some functions that are simple:

 #include <stdio.h>  char  *fun1 (char  *p, char  a[]) {printf  (     "fun1 () \ n" ); return  NULL;} char  *fun2 (char  *p, char  a[]) {printf  (     "fun2 () \ n" ); return  NULL;} char  *fun3 (char  *p, char  a[]) {printf  (     "fun3 () \ n" ); return  NULL;}  

(a) Declare a function pointer, pointing to the function above.

At this point the main function is implemented as:

int main (){    char"hello";    char"world";    char *(*pfun)(char *p,char a[]);    pfun = &fun1;    (*pfun)(pp,arr);    printf("\n");    return0;}

The 5th line in the main function:

char *(*pfun)(char *p,chara[]);

let us briefly analyze, starting from the left parenthesis, we can see that this is a pointer to the function, the function has two char parameters, the function return value is char type.
The 6th exercise function pointer points to our custom fun1 function.
The 7th line is to call our fun1 function through a function pointer. It is important to note that the dereference symbol for this line can be saved without affecting the result, and the reference symbol is known to make it clear that we are calling the FUN1 function.

(b) Declare an array of function pointers to hold pointers to the above functions (FUN1, etc.).

At this point the main function is implemented as:

intMain () {Char*PP ="Hello";CharArr[] ="World";inti =0;Char* (*pfunarr[3])(Char*p,CharA[]); pfunarr[0] = FUN1; pfunarr[1] = &fun2; pfunarr[2] = FUN3; for(i=0; i<3;    i++) {(*pfunarr[i]) (Pp,arr); }printf("\ n");return 0;}

You must have been a little dizzy ...

The 6th line in the main function:

char *(*pfunarr[3])(char *p,chara[]);

Let's analyze it again:

Start analysis from the left parenthesis, because the subscript reference is higher than the indirect access, so this is a 3-element array, each element of the array is a pointer, each pointer to a function, the function has two char parameter, the function return value is char type.

Then:

    pfunarr[0] = fun1;    pfunarr[1] = &fun2;    pfunarr[2] = fun3;

You will find out why the other two functions do not have an add-on character? We usually say that a variable has its corresponding address, and seldom hear that a function has its address. Perhaps the address of a function is in that area. Therefore, the above address Fukoga also can not be added, will not affect the results.
This piece we go through the subscript reference to access 3 functions in turn.

(iii) Declaring a pointer to an array of function pointers above

At this point the main function can be implemented as:

int main (){    char"hello";    char"world";    char *(*pfunarr[3])(char *p,char a[]);    char *(*(*pppfun)[3])(char *p,char arr[]);    pppfun = &pfunarr;    (*pppfun)[0](pp,arr);    (*pppfun)[1](pp,arr);    (*pppfun)[2](pp,arr);    return0;}

The 6th line in the main function:

char *(*(*pppfun)[3])(char *p,char arr[]);

Let's analyze it again:

Starting from the innermost parentheses on the left, the indirect access plus the parentheses precedence is higher than the subscript reference, so this is a pointer to an array of 3 elements, each element of the array is a pointer, each pointer (element) points to a function, and the function has two char parameters. The return value of the function is of type char .

To merge the main function code:

intMain () {Char*pp="Hello"; Char arr[] ="World";inti =0; Char*(*pfun) (Char*p, Char a[]); Char*(*pfunarr[3]) (Char*p, Char a[]); Char*(*(*pppfun)[3]) (Char*p, Char arr[]);    Pfun = &fun1; (*pfun) (Pp,arr);printf("\ n"); pfunarr[0] = FUN1; pfunarr[1] = &fun2; pfunarr[2] = FUN3; for(i=0; i<3; i++) {(*pfunarr[i])    (Pp,arr); }printf("\ n");    Pppfun = &pfunarr; (*pppfun)[0] (Pp,arr); (*pppfun)[1] (Pp,arr); (*pppfun)[2] (Pp,arr);return 0;}

"C-language" functions, function pointers, arrays of pointers, array pointers.

Related Article

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.