Define the pointer of a function. The difference between C ++ and C is that the function is overloaded.

Source: Internet
Author: User

In C, a pointer can point to a function. This pointer also has two attributes, but one is the function entry address and the other is the function return value type. For example, the following program is correct in C:
Int time12 (int I)
{Return (I % 12 );
}
 
Int main ()
{Int (* fp) () = time12;
Intt = fp (13 );
Return0;
}
The first sentence of the main function is a Definition Statement. We should start from the identifier on the left of the equal sign. All types of symbols except the identifier on the left of the equal sign must be read in the read order according to the operation level when the symbol is used as the operator. Fp is a pointer pointing to a function (Note: C language allows its parameter type not to be written) the return value of this function is int, And the pointer is initialized as the entry address of the function time12.
However, the first statement of the above program in C ++ is regarded as wrong. C ++ is a strongly typed check language, which is related to the function overload mechanism of C ++. C ++ requires that all types of formal parameters of the function be specified. The following program is the correct C ++ program:
Int time12 (int I)
{Return (I % 12 );
}
 
Int main ()
{Int (* fp) (int) = time12;
Intt = fp (13 );
Return0;
}

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.