pointer functions and function pointers

Source: Internet
Author: User

pointer function :

Refers to a function with pointers, essentially a function, and the return type of a function is a pointer to a type.

1 int    *fun (int a);

Since the function call operator () takes precedence over the indirect operator *, the first thing to do is a function call operation, so it is a function, except that the return value of the function is an address value, and the return value of the function must be accepted by a pointer variable of the same type, that is, Pointer functions must have return values for functions, and in the keynote function, the function return value must be assigned to a pointer variable of the same type.

1 float    * Fun(); 2 float    *p; 3 p = Fun (a);

function Pointers :

A pointer variable that points to a function, which is essentially a pointer variable. The two most common uses are conversion tables (jump table) and passing them as parameters to another function.

1 int    (*fun) (int a);      // declaring a function pointer

Simply declaring a pointer to a function does not mean that it can be used immediately, as with other pointers, you must initially point to a function before performing an indirect access to the function pointer.

1 int    F (int); 2 int    (*PF) (int ) = &f;

The second declaration creates the function pointer pf and initializes it to the function f, which can be implemented by a simple assignment statement.

The & operator in the initialization expression is optional, because the function name is always used by the compiler to convert it to the function pointer,& operator, just to show that the compiler will perform the task implicitly.

The function pointer is initialized, there are three ways to make a rake:

1 int     ans; 2 ans = f (+); 3 ans = (*PF); 4 ans = PF (+);

The first assignment statement is to invoke a function with a name, and the process of executing it is that the function name F is first converted to a functional pointer, which points to the position of the function in memory, and then the function call operator calls the function, which executes starting at this address code.

The second statement is to perform an indirect access operation on PF, which converts the function pointer to a function name, which is not really needed because the compiler will convert it before executing the function call operator, and the effect is the same as the first statement.

The third statement is that the indirect access operation is not required, because the compiler needs a function pointer, and of course the effect is the same as the previous two.

pointer functions and 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.