The difference between C and C + + function pointers as function parameters

Source: Internet
Author: User

1. function as parameter plus * and no *

Example 1:

void print (int i) {    printf ("Not parameter%d\n", i);     } void Debugfun (void <span style= "color: #ff0000;" > (*funprint</span>) (int)) {    funprint (2);    Return     


Main () {         debugfun (print);}


Output:
Not parameter2

Example 2: (Remove the red part * number)

void print (int i) {    printf ("Not parameter%d\n", i);     } void Debugfun (void <span style= "color: #ff0000;" > (funprint</span>) (int)) {    funprint (2);    Return     


Main () {         debugfun (print);}


The output is the same as:
Not parameter2

Conclusion: The function pointer is the same as the formal parameter and does not add * meaning

The degree of rigor between 2.C and C + + for the type of function

C Example:

typedef struct REGISTER  {      int WR0;            int wr1[4];      int wr2[4];      int wr3[4];      int WR4;        int WR5;  } Register;


void print (<span style= "color: #000000;" >register reg</span>)  {      printf ("Data entry is%d \ n", Reg.) WR0);  }  

void Listtraverse (void (*visit) (<span style= "color: #ff0000;" >Register</span>))   {  
         
         Reg. WR0 = 2;          Visit (reg);             }  
Main () {    listtraverse (print);}

Output:

Data item is 2

If you remove the red part Register, and the Red part visit (reg), change to visit (Reg. WR0), after, incredibly not error can also be correctly output;

Because the compiler automatically will be from Reg. WR0 the beginning of the content as the starting point of a register type (the length of sizeof (Register)) is converted to the register type as a function of the formal parameter

The following routines can prove what I said.

If you change into

void Listtraverse (void (*visit) ())   {  
         
         Reg. WR4 = ten;          Visit (<span style= "color: #ff0000; Background-color: #f0f0f0 ">reg. wr4</span>);        }  
The output is:
Data item is 10
    Obviously before the Reg. The content of the WR4 unit becomes the starting unit of the register type that is newly converted to
C + + Routines:
    According to the code in the C routine compiler compiled with C + + compiler, if the parameters inside the pointer function does not add type identification, the program directly error
In summary, C and C + + are more stringent in the function pointers as function parameters to the formal parameter requirements of the function.


The difference between C and C + + function pointers as function parameters

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.