C/C ++ function pointer, usage and use of pointer Functions

Source: Internet
Author: User

First look at the function pointer int func2 (int x);/* declare a function */int (* q2) (int x);/* declare a function pointer */q2 = func2; /* assign the first address of the func function to the pointer f */int c = (* q2) (3 ); // 3 the usage of the function pointer can be basically illustrated for any real parameter in the above four sentences: The function pointer is a pointer to the function, to be exact, is a pointer to a specific type of function (the function and the function pointer type must match). The function pointer is used to save the first address of the function, that is, the function can be accessed through this pointer. Function pointerEquivalent to alias. Function pointers can point to a type of function, rather than a function, that isYou can assign a value again.. (See example 2) it is not difficult to use a function pointer. Just remember the black text above. Attach two simple examples: max (x, y) {(x> y? (* Ptr) (, =, & a, & = (*View Code

Example 2:

(* =View Code

 

Look at the pointer function again: int * func (int x);/* declare a pointer function */int * q;/* declare a pointer */q = func (3 ); // 3 the pointer function can be described in the preceding three statements for any real parameter: A pointer function is a function that returns a pointer.So we can use a pointer of the same type to receive it when calling it in main. Pointer functions can be used to solve many problems, such as the problem of returning multiple values.. (See the article "function return methods with multiple values) Pointer functions are more frequently used, You must learn to use pointer function logic is simple, the structure is fresh, powerful, good quality, high efficiency, comparable to the best Fengjie, better... (For downlink), it is really a loss if you do not learn it. Example 3 is also from Baidu. I personally think it is classic. It involves pointer, array pointer, pointer function, and two-dimensional array assignment. The function returns multiple values, the difference between auto-increment of array pointer and auto-increment of pointer... Numerous knowledge. You can understand this basic pointer. Example 3:
#include <iostream>
   * find ((* p) [], m);
  score [] [] = {{,,,}, {,,,}, {,,,}};
  * pf = NULL;

>> = find (score, m);
(i =; i <; i ++ << * (pf + i) << << * find ((* p) [], * pf == * (p + m); 

Pf = * (p + m); // p is a pointer to a two-dimensional array, with * pointer to a one-dimensional array

This sentence is classic.


Related Article

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.