C Foundation--the use of function pointers

Source: Internet
Author: User
Tags define function prototype definition

Before looking at the code, look at the use of function pointers, broadly divided into the following categories:

    • A function list, called by the pointer Index, makes functions with similar functions look clearer;
    • A function pointer is used as a callback for another function's argument;
    • Linux is often used to achieve the same interface, the implementation of different, such as:
1 structPlatform_driver {2     int(*probe) (structPlatform_device *);3     int(*remove) (structPlatform_device *);4     void(*shutdown) (structPlatform_device *);5     int(*suspend) (structPlatform_device *, pm_message_t state);6     int(*resume) (structPlatform_device *);7     structDevice_driver driver;8     Const structPLATFORM_DEVICE_ID *id_table;9     BOOLPrevent_deferred_probe;Ten};

The implementation can be the serial port of the Freescale IMX series chip:

1 Static structPlatform_driver Serial_imx_driver = {2. Probe =Serial_imx_probe,3. remove =__devexit_p (serial_imx_remove),4 5. Suspend =Serial_imx_suspend,6. Resume =Serial_imx_resume,7. id_table =Imx_uart_devtype,8. Driver = {9. Name ="Imx-uart",Ten. Owner =This_module, One. of_match_table =Imx_uart_dt_ids, A     }, -};

Here is an excerpt from a blog:

Transferred from: http://blog.sina.com.cn/s/blog_4d48cc5d0100xnh9.html

Use:

Function pointers are often used to implement callbacks, or they can be used to optimize a module invocation in the form of a function table.

How to use:

1. Define function pointer type

more intuitive and easy to use with typedef

1 // A function pointer that defines a prototype as an int fun (int a); 2 int int Apara);

The function of a typedef is to define a new type. This defines a type of Ptrfun and defines the type as a pointer to a function that takes an int as a parameter and returns a char type. You can use the ptrfun in the same way as with Int,char .

2. Definition of function pointer variable

1 // Pfun is a function pointer variable name 2 int int // pFun2 is also a function pointer variable name

3. Function pointers are passed as parameters of functions

1 //Defining callback Functions2 intCallBack (inta)3 {4     return++A;5 }6 7 //defining a callback function8 voidCaller (Ptrfun CB)//void Caller (int (*CB) (int))//It can also be stated that9 {Ten     intNpara =1;  One     intNret =CB (Npara); A } -  -  the //using callbacks - voidMain () - { -Caller (CallBack);//using callback functions directly +Ptrfun cb = CallBack;//Int (*CB) (int); cb = CallBack; -     intNRet1 = CB ( About);//NRet1 = +; +}

4, pointer to function pointer use

1 //pointers for defining function pointers2typedefint(**ptrptrfun) (intApara);3 4 //pointer to function pointer as parameter5 //void Ptrcaller (ptrfun* cb)//Pointer Declaration6 //void Ptrcaller (int (**CB) (int))//Prototype Declaration7 voidPtrcaller (ptrptrfun CB)8 {9     intNret = (*CB) (999);//nret = +;Ten } One  A //using pointers to function pointers - voidMain () - { thePtrfun cb =CallBack; -Ptrcaller (&CB); -}

5. Use of array of function pointers

1 //definition of an array of function pointers2Ptrfun farray[Ten]3 //Int (*farray[10]) (int);//prototype Definition4  for(inti =0; I <Ten; i++ )5 {6Farray[i] =CallBack;7     intNret = Farray[i] (i);//nret = i+1;8}

6. The size of the function pointer

Since it's called a pointer, it's the same size as a normal pointer in a 4 -bit system.

1 int sizeof // nSz1 = 4; 2 int sizeof // nSz2 = 4;

Usage examples:

Here is a simple example of using a function pointer:

1 //------------------------------------------------------2#include <stdio.h>3 4typedefint(*ptrfun) (intApara,intBpara);5 6 intFunc1 (intPaintPb);7 intFunc2 (intPaintPb);8 intFUNC3 (intPaintPb);9 intFunc4 (intPaintPb);Ten  One voidMain () A { -     inti =0; -     intj =0; thePtrfun pfuncarray[4] ={func1, Func2, func3, func4}; - Ptrfun PFunc; -  -      for(i =3; I >=0; I--) +     { -PFunc =Pfuncarray[i]; +printf"Output is:%d \ n", (*PFunc) (I,i)); A     } at } -  - intFunc1 (intPaintPb) - { -printf"func1 with input para%d,%d \ n", Pa, Pb); -     returnPa +Pb; in } -  to intFunc2 (intPaintPb) + { -printf"FUNC2 with input para%d,%d \ n", Pa, Pb); the     returnPa-Pb; * } $ Panax Notoginseng intFUNC3 (intPaintPb) - { theprintf"func3 with input para%d,%d \ n", Pa, Pb); +     returnPa *Pb; A } the  + intFunc4 (intPaintPb) - { $printf"FUNC4 with input para%d,%d \ n", Pa, Pb); $     return(Pb = =0)?0xFFFF: (Pa/Pb); -}

The output is:

1Func4 with input para3,32Output is:13 4FUNC3 with input para2,25Output is:46 7Func2 with input para1,18Output is:09 TenFunc1 with input para0,0 OneOutput is:0

C Foundation--the use 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.