(This article is transferred from the Internet: I have doubts about the point of view in this article, but the specific reasons are not mentioned yet, and the point of view in this article cannot be persuaded. But there is no way to do it. Keep it first and then gradually improve it! However, some people say:
The function name is the function name, but during the compilation process, when the function name is used, the compiler always converts it to the function pointer, the usage only shows that the tasks that the compiler will execute implicitly are justified. However, my current knowledge cannot confirm whether the tasks are correct, we look forward ....................................
)
Function name and function pointer
I. Common function calls
An example of a common function call:
// Self-contained header files
Void myfun (int x); // The statement can also be written as void myfun (INT );
Int main (INT argc, char * argv [])
{
Myfun (10 );// Call myfun (10 ).
Return 0;
}
Void myfun (int x) // A myfun function is defined here.
{
Printf ("% d \ n", X );
}
This myfun function is a function without a return value and does not accomplish anything. You should be familiar with this function calling format! View the writing format of the myfun function called in the main function:
Myfun (10 );
At the beginning, we only understood the function myfun in terms of functionality or mathematics. We knew that the myfun function name represents a function (or a piece of code ).
Until --
When learning the function pointer concept. I had to think: what is the function name?
(Don't think this is meaningless. Oh! You can see it later .)
Declaration of binary function pointer Variables
Just as the memory address of a Data variable can be stored in the corresponding pointer variable, the first address of the function is also stored in a function pointer variable. In this way, I can call the function pointed to through this function pointer variable.
In the C series language, any variable must be affirmed before it can be used. So should the function pointer variable be affirmed first? How can we declare it? In the preceding example, I declare a function pointer variable funp that can point to the myfun function. The following describes how to declare the funp variable:
Void (* funp) (INT); // You can also write it as void (* funp) (int x );
You can see that the declaration format of the entire function pointer variable is the same as that of the function myfun, but we just changed myfun to (* funp, in this way, there is a pointer funp that can point to the myfun function. (Of course, this funp pointer variable can also point to all other functions with the same parameters and return values .)
3. Call a function using the function pointer variable
With the funp pointer variable, we can assign values to myfun and call the myfun function through funp. Let's see how I call the myfun function through the funp pointer variable:
// Self-contained header files
Void myfun (int x); // This statement can also be written as void myfun (INT );
Void (* funp) (INT );// It can also be declared as void (* funp) (int x), but this is generally not the case.
Int main (INT argc, char * argv [])
{
Myfun (10); // This is the direct call to the myfun Function
Funp = & myfun;// Assign the address of the myfun function to the funp variable
(* Funp) (20 );// This is to call the myfun function through the function pointer variable funp.
}
Void myfun (int x) // A myfun function is defined here.
{
Printf ("% d \ n", X );
}
See the code and comments in the black text section.
Run it. Well, it's good. The program runs well.
Oh, my feeling is: the relationship between myfun and funp is similar to that between int and int. The function myfun seems to be an int variable (or constant), while funp is a pointer variable like int.
Int I, * PI;
Pi = & I; // compare with funp = & myfun.
(How do you feel ?)
Well, it's not actually --
4. Other writing formats for calling Functions
Function pointers can also be used as follows to accomplish the same thing:
// Self-contained header files
Void myfun (int x );
Void (* funp) (INT); // declare a pointer variable used to point to the same parameter, return value function.
Int main (INT argc, char * argv [])
{
Myfun (10); // call myfun (10) here; Function
Funp = myfun; // assign the address of the myfun function to the funp variable.
Funp (20); // This is used to call the myfun function through the function pointer variable.
Return 0;
}
Void myfun (int x) // A myfun function is defined here.
{
Printf ("% d \ n", X );
}
I changed the black text part (Please compare it with the previous Code ).
Run it! Same success.
Why?
Funp = myfun;
In this way, we can assign the same value of myfun to funp. Is myfun and funp the same data type (like the relationship between int and INT), rather than the relationship between int and int? (Are you confused ?)
It seems a little different from the previous code, right! That's why I said it!
Please let me not explain it to you for the time being. continue to look at the following situations (these can be code that can be correctly run !) :
Code 3:
Int main (INT argc, char * argv [])
{
Myfun (10); // call myfun (10) here; Function
Funp = & myfun; // assign the address of the myfun function to the funp variable.
Funp (20); // This is used to call the myfun function through the function pointer variable.
Return 0;
}
Code 4:
Int main (INT argc, char * argv [])
{
Myfun (10); // call myfun (10) here; Function
Funp = myfun; // assign the address of the myfun function to the funp variable.
(* Funp) (20); // This is used to call the myfun function through the function pointer variable.
Return 0;
}
This is really the case!
(Wow! I really want to faint !)
What else! View --
Int main (INT argc, char * argv [])
{
(* Myfun) (10); // you can call the function name myfun in this format.
Return 0;
}
You may see it for the first time: function name calling can also be written like this! (It's just that we do not normally write like this .)
So what are the explanations?
Haha! If I were Sherlock Holmes, the new discovery in this article will be inferred based on previous knowledge and experience, and the following conclusions will be inferred:
1. In fact, the function name of myfun is the same as that of funp, that is, all function pointers. The myfun function name is a function pointer constant, and funp is a function number pointer variable, which is their relationship.
2. However, if function names are called like (* myfun) (10), it is inconvenient to write and read. Therefore, the designers of C language can design and allow myfun (10); this form of calling (this is much more convenient and the function form in mathematics is the same, isn't it ?).
3. for uniformity, funp function pointer variables can also be called in the form of funp (10.
4. When assigning values, you can use funp = & myfun, or funp = myfun.
Whatever you like about the above Code!
Please understand this! This helps you to apply function pointers!
Last --
Note: In the Declaration of the function:
Void myfun (INT); // cannot be written as void (* myfun) (INT ).
Void (* funp) (INT); // It cannot be written as void funp (INT ).
(Please refer to the notes.
5. Define the pointer type of a function:
Just like a custom data type, we can also define a function pointer type, and then use this type to declare the function pointer variable.
Let me give you an example of a custom data type.
Typedef int * pint; // defines a pint alias for the int * type.
Int main ()
{
Int X;
Pint PX = & X; // It is equivalent to int * PX = & X. The pint type is actually the int * type.
* PX = 10; // PX is an int * type variable.
Return 0;
}
According to the notes, it is not difficult to understand it! (Although you may rarely use this definition, it will often be seen later when learning Win32 programming .)
Next, let's take a look at the definition and usage of the function pointer type: (Please compare with the above !)
// Self-contained header files
Void myfun (int x); // The statement can also be written as void myfun (INT );
Typedef void (* funtype) (INT); // This is just to define a function pointer type
Funtype funp; // use the funtype type to declare the global funp variable.
Int main (INT argc, char * argv [])
{
// Funtype funp; // The function pointer variable can also be local, so please declare it here.
Myfun (10 );
Funp = & myfun;
(* Funp) (20 );
Return 0;
}
Void myfun (int x)
{
Printf ("% d \ n", X );
}
Look at the simhei part:
First, a typedef is added before void (* funtype) (INT. In this way, only a pointer type named funtype is defined, instead of a funtype variable.
Then, the funtype funp; statement declares a funp variable like pint PX.
Others are the same. The entire program has done the same thing.
The advantages of this method are:
With the funtype type, we can easily declare multiple function pointer variables of the same type with the funtype type. As follows:
Funtype funp2;
Funtype funp3;
//......
Six function pointers are used as parameters of a function.
Since the function pointer variable is a variable, it can also be used as a function parameter. Therefore, you should also know how function pointers are transmitted and used as parameters of a function.
Here is an example:
Requirement: I want to design a callmyfun function. This function can call the myfun1, myfun2, and myfun3 functions by using different function pointer values in the parameter. (Note: the format of the three functions must be the same ).
Implementation: The Code is as follows:
// Self-contained header files
Void myfun1 (int x );
Void myfun2 (int x );
Void myfun3 (int x );
Typedef void (* funtype) (INT); // ②. Define a function pointer type funtype.
Void callmyfun (funtype FP, int X );
Int main (INT argc, char * argv [])
{
Callmyfun (myfun1, 10); // ⑤. Use the callmyfun function to call three different functions.
Callmyfun (myfun2, 20 );
Callmyfun (myfun3, 30 );
}
Void callmyfun (funtype FP, int X) // ③. the type of the parameter FP is funtype.
{
FP (x); // ④. Execute the passed function through the FP pointer. Note that the function referred to by FP has a parameter
}
Void myfun1 (int x) // ①. This is a function with a parameter. The following two functions are the same.
{
Printf ("output in the function myfun1: % d \ n", X );
}
Void myfun2 (int x)
{
Printf ("output in the function myfun2: % d \ n", X );
}
Void myfun3 (int x)
{
Printf ("output in function myfun3: % d \ n", X );
}