Pointer to function (function pointer) and pointer Function

Source: Internet
Author: User

In the C language, it is easy for people to get dizzy when they encounter pointers. Especially, although I know the pointer myth and the C language, I also know the trouble of pointers.

Today, when I read a book and saw a pointer to a function, I suddenly fell into a self-chaos state. This is an escape of psychological chaos! But I always need to understand it slowly, so I will try my best to summarize it based on what I see on the Internet and what I have read in the book, so that I can refer to it later...

In C, the function itself is not a variable, but can define a pointer to the function. Pointers of this type can be assigned values, stored in arrays, passed to functions, and returned values as functions. This basically shows why a function pointer is required. In my understanding, a function itself cannot be used as a function parameter, So we define a pointer to the function so that it can be used as a parameter in the function. This may be the most important use of function pointers!

Baidu encyclopedia understands that a function pointer is a pointer variable pointing to a function. Therefore, the "function pointer" should be a pointer variable first, except that the pointer variable points to the function. This is just like using pointer variables to point to integer variables, struct variables, and arrays. Here we point to functions. As mentioned above, during C compilation, each function has an entry address, which is the address pointed to by the function pointer. With the pointer variable pointing to the function, the function can be called using the pointer variable, just like using the pointer variable to reference other types of variables. These concepts are consistent. Function pointers have two purposes: Calling functions and making function parameters.

Note 1: "function type" indicates the return type of the function. Because "()" has a higher priority than "*", parentheses outside the pointer variable name are required, the following "parameter list" indicates the parameter list of the function to which the pointer variable points. For example:

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.