void (*f (int, void (*) (int))) (int) function resolves to

Source: Internet
Author: User

Today with a few classmates see a function pointer definition:

void (*f (int, void (*) (int))) (int)

Previously seen in the C trap pit fails inside, but the article is described in detail, but often so that beginners do not grasp the focus, the result of a sewage. Here is a brief introduction to the definition of function pointers in this way.

what is a function pointer?

This problem is well understood from a definition point of view, pointers to functions are function pointers, but how do we declare a function pointer? And how do you cast an address to a function pointer of one type? Here is an example of the source code:

void function (int a)

{

A = 5;

}

void (*PFUNC) (int);

Quite simply, the above code declares a function fucntion and a function pointer Pfunc, which refers to a function that has a void return value, an int parameter. If the address of the function functions to the Pfunc pointer, you can simply pass the following two kinds of assignment:

Pfunc = function;

Or

Pfunc = &function;

There are two ways to call this function through pointers:

Pfunc (5); or (*pfunc) (5);

Let's look at the assignment statement, pfunc = function; But sometimes it can be a constant 0x8999940, which happens to also represent a safe function with functions, how to assign this value to Pfunc? Obviously we need to enforce the type conversion, what type of the constant should be converted to? This is the key to the problem!

In a void (*PFUNC) (int) statement, only Pfunc is the variable name, then the remainder, void (*) (int), is the type of transformation we need. Therefore, the new assignment statement is:

Pfunc = (void (*) (int)) 0x8999940;

Once the assignment is complete, it can be passed Pfunc (5); or (*pfunc) (5); call the corresponding function.

If we understand the above, we can explain the relatively complex problem of void (*signal (int, void (*) (int)))) (int).

functions that return function pointers

Now let's leave the complex definition above, first look at the following requirements 1) define a function, 2) The function has the following characteristics, two parameters, the return value is a function pointer, and a parameter is also a function pointer. If the return value and the parameter function pointer are the same as void (*) (int); Another function parameter is the INT type. The function defines a name of My_func.

We can easily define this function according to the requirements:

typedef void (*handler) (int); parameter functions and return function definitions

HANDLER my_func (int, HANDLER);

The sudden need to use typedef is not allowed, this is the case that the early C language does not support TypeDef, then how to define this function?

If we say that the return value of My_func is int, it is not the definition that can be written like this:

int my_func (int, void (*) (int));

That is, my_func (int, void (*) (int)) is a type of int data. Now replace int with a function, which is

Void (*) (int) my_func) (int, void (*) (int);

Such a definition, obviously this syntax does not support, then, how is the actual expression? Looking back, let's take a look at the declaration format of the function pointer

void (*pfunc) (int) ;

Where Pfunc is equivalent to void (*) (int). Now look at the above format, is not very familiar with, right, Pfunc is my_func (int, void (*) (int)). Now if you replace the two, this is the format:

void (*my_func (int, void (*) (int))) (int )

If we change my_func to signal, is that the complex fame we started with? Is it clear now, that's it? It's a function of returning a function pointer!

void (*f (int, void (*) (int))) (int) function resolves to

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.