A pointer to an in-depth parse function pointer and return function

Source: Internet
Author: User
The following is a detailed analysis of the function pointer and the return function of the pointer, the need for friends can come to refer to the next

Let's take a look at the following two codes:
1: Originating from Stl-sgi source <stl_alloc.h>

Copy Code code as follows:


static void (*__set_malloc_handler (*__f) ()) ()


{


void (*__old) () =__malloc_alloc_oom_handler;


__malloc_alloc_oom_handler=__f;


return (__old);


}


<sginal.h> under the 2:linux

Copy Code code as follows:


void (*signal (int sig_num, void (*handler) (int)) (int)


As a C + + master may be a look at it, the following to analyze, the analysis of the method used is from the inside out of the analytical method

For example 1 analysis is as follows:
1 The innermost is void (*_f) () The _f is a pointer, combined with the right () to indicate that it is a function, in conjunction with the leftmost void, indicating that it is a function pointer.

2) using a typedef to define void (*_f) () to facilitate the analysis of the following

Typedef void (*f) ()

3 So the code above can be turned into the following form
Static Void (*__set_malloc_handler (f)) ()

4 and then from the outside analysis __set_malloc_handler (f F) First and () combined to indicate that it is a function, the parameter type F, and then to the left of the * binding, indicating that the function returns a pointer type, that is, static void (*) (), so is a function pointer

5) can get the code function named __set_malloc_handler, accept a return value is NULL, the parameter is NULL function pointer as a parameter, and finally return a function pointer, the same return value and parameters are null, very around the mouth:

For example 2 analysis is as follows
1 from inside to outside is void (* handler) (int) Read the 1, it is easy to know that this is a function pointer, the return value is null, the argument is empty

2 to make a macro definition of void (*handler) (int) to facilitate understanding
Typedef Void (*handle) (int)

3) So the function becomes void (*signal (Intsig_num,handler Handler)) (int)

4 from the Inside Outward analysis signal (int sig_num,handlerhandler) is a function, with two parameters, the return value is *, that is a pointer, the return value of void (*) (int) is also a function pointer of a handler type

5 To sum up, the function name of code 2 is signal, accept two parameters, return a function pointer of handler type.

Other code analysis is similar to the following examples:

Copy Code code as follows:


1:void * (* (*f) (int)) [10];


analysis is as follows (* (*F) (int)-&gt;void * (*) [10]


2:void * (*f) (int) [10]


analysis is as follows (*F) (int)-&gt;void *[10]


And some of them are basically similar deformations.

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.