C Language Study-function pointer usage

Source: Internet
Author: User

C Language Study-function pointer usage
Function pointer usage

 

1. function pointer Definition

The first time I used the function pointer, I declared it like this,Typedef void (* pSimulatepks) (uint8 * prxBuf, uint8 * prxBufLen );

The first bracket () is used to package pSimulatepks into a pointer, and the second bracket () is a function parameter list. Therefore, pSimulatepks is a function pointer, pointer to such a type of function.

A function pointer is declared above, so that a function pointer can be defined in this way,PSimulatepks pfun;

 

2. function pointer call Function

 

For example, a function isVoid fun (uint8 * prxBuf, uint8 * prxBufLen );You can do this,Pfun = fun;

You can call fun later,Pfun (rxBuf, & rxBufLen );Or(* Pfun) (rxBuf, & rxBufLen );


3. function pointer as function return value

PSimulate simFunc (uint8 cmd)

{

Switch (cmd)

{

Case 0x01:

Return sim_a;

 

Case 0x02:

Return sim_ B;

Case 0x03:

Return sim_c;

Case 0x04:

Return sim_d;

}

}

Among them, sim_a sim_ B sim_c sim_d is the function name of this class function.

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.