Detailed description of C ++ function pointers and function pointers

Source: Internet
Author: User

Detailed description of C ++ function pointers and function pointers

In the process of learning c ++, pointers are difficult. After getting familiar with pointers, there is also a tough problem, that is, function pointers. This blog introduces the common function pointers in detail.

For pointer details, we recommend this blog article C ++ pointer details

Like data, the function also has an address. The function address is the starting address for storing the function language code in the memory. The function pointer points to this address. The Type pointed to by the function pointer is the function itself. We know that the pointer type indicates the size of the memory area pointed to by the pointer. So the type pointed to by the function pointer is the memory size occupied by the function in the memory. Knowing the starting address and size of a function, function pointers can easily complete function calls instead of functions.

I. simplest function pointer

Variables include declarations and assignments. Pointers are no exception, and function pointers are no exception. Let's look at a simple function:

void add(int a, int b){    cout << a + b << endl;}

A simple addition calculation and output to the command line function.

So how to call it through the function pointer?

1. Statement:
void (*p1)(int a, int b);

The declaration of function pointers is very simple. Basically, a pointer is used to replace the function name. The type of pointer p1 is void (*) (int a, int B), indicating that the pointer is a pointer to a function, and the pointer to the type is void () (int, int B)

2. assignment:
p1 = add;
3. You can also directly define:
void (*p1)(int a, int b) = add;  

Note: The function name add of the void add (int a, int B) function is the address of the function. Assign the address add value to pointer p1, then you can directly call the function through the function pointer p1.

4. call:
(*p1)(1, 2);p1(1, 2);

Note! For historical reasons, the above two methods can call functions.

 

2. array containing multiple function pointers

Sometimes, in this case, there is an array. every element in the array is a function pointer. How can we define this array?

1. Explanation * p [n] and (* p) [n]

We know that the [] operator has a higher priority than *. Therefore, p [3] indicates an array containing three elements, * p [3] indicates the type of elements in the array. * p [3] indicates an array pointing to three pointers.

P [3] indicates an array containing three elements, so (* p) [3] replaces p with * p, which is easy to think of, (* p) [3] indicates a pointer to an array containing three elements.

2. Statement:
void (*p2[2])(int a, int b);

The array name is p2 and the array size is 2. The element type in the array is void (*) (int a, int B), indicating that the element is a pointer to a function, the pointer is of the void () (int a, int B) type ).

3. assignment:
p2[1] = add;

It is the same as above.

4. call:
p2[1](2,3);(*p2[1])(3,4); 

You can use either of the two methods.

 

3. pointer to "array containing multiple function pointers"

This title seems to be a bit closed. In short, this pointer points to the above "array containing multiple function pointers ". In fact, it is very simple. To put it bluntly, p2 in the above section is replaced by a pointer.

1. Statement:
void (*(*p3)[2])(int a, int b);

We can see that p2 is replaced by * p3.

2. assign values. Note that, since it is a pointer, it must be initialized before use:
p3 = &p2;
(*p3)[1] = add;

Note! Since p2 is replaced by * p3 in essence, c ++ 11 can be easily defined as follows: auto p3 = & p2; replaces void (* p3) [2]) (int a, int B) = & p2;

3. call:
(*p3)[1](1, 2);(*(*p3)[1])(1, 2);

 

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.