Void (* f (int, void (*) (int) function parsing, voidint

Source: Internet
Author: User

Void (* f (int, void (*) (int) function parsing, voidint
Function pointer

 

Today I saw a function pointer definition with several students:

 

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

 

I have seen it in C trap pit fails in the past, but the articles are very detailed, but they often make it difficult for beginners to grasp the key points and get a sewage. Here we will briefly introduce the function pointer definition method.

 

What is a function pointer?

 

This problem is well understood from the definition perspective. The pointer to a function is a function pointer, but how do we declare a function pointer? How can we forcibly convert an address to a function pointer of a certain type? The following example shows the source code:

Void function (int)

{

A = 5;

}

Void (* pfunc) (int );

 

The above Code declares a function fucntion and a function pointer pfunc. It points to a function that has the void return value and the int parameter. If you give the function address to the pfunc pointer, you can simply assign values through the following two types:

Pfunc = function;

Or

Pfunc = & function;

You can call this function using pointers in either of the following ways:

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

Let's take a look at the value assignment statement, pfunc = function; but sometimes it may be a constant of 0x8999940, which exactly indicates a safe function with the same function as function, how can I assign this value to pfunc? Obviously, we need to force type conversion. What type should we convert this constant? This is the key to the problem!

In the void (* pfunc) (int) Statement, only pfunc is the variable name. The rest part, void (*) (int), is the required conversion type. Therefore, the new value assignment statement is:

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

After the assignment is complete, you can call the corresponding function through pfunc (5); or (* pfunc) (5.

 

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

 

Returns the function name of the function pointer.

 

Now let's move away from the complex definition above. First, let's look at the requirements below. 1) define a function. 2) This function has the following features: two parameters, and 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), the other function parameter is of the int type. The Function Definition name is my_func.

 

We can easily define this function as needed:

 

Typedef void (* HANDLER) (int );//Parameter functions and return functions

HANDLER my_func (int, HANDLER );

 

Suddenly, typedef is not allowed to be used in the demand. This is because typedef is not supported in the early C language. How can this function be defined?

Let's say that the returned value of my_func is int. Can it be defined as follows:

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

That is to say, my_func (int, void (*) (int) is an int type data. Replace int with a function, that is

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

This definition is obviously not supported by this syntax. How is it actually expressed? Let's look at the Declaration format of the function pointer.

Void (* pfunc) (int);

Pfunc is equivalent to void (*) (int ). Now let's see if the above format is familiar. By the way, pfunc is my_func (int, void (*) (int )). If you replace the two, is it in this format:

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

If I replace my_func with signal, is it the complicated reputation we mentioned at the beginning of this article? Do you understand it now? It turns out to be a function that returns function pointers!

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.