C Function Type and function pointer usage

Source: Internet
Author: User
2. Common function calls

An example of a common function call:
/* Self-Contained header files */
Void myfun (int x);/* The declaration here can also be written as void myfun (INT )*/
Int main (INT argc, char * argv [])
{
Myfun (10);/* call the myfun (10) function */
Return (0 );
}
Void myfun (int x)/* defines a myfun function */
{
Printf ("% d \ n", X );
}
This myfun function is a function without a return value and does not "complete" 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 the function pointer concept is learned. I had to think: what is the function name?

(Don't think this is meaningless. Oh! You can see it later .)

Ii. Declaration of 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 declared before it can be used. So should the function pointer variable be declared first? How can we declare it? In the preceding example, we 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);/* can also be written 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, except that we 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);/* 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 );/*(★) The function pointer variable funp is used to call the myfun function. */
}
Void myfun (int x)/* defines a myfun function */
{
Printf ("% d \ n", X );
}
See (★. 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;/* compared with funp = & myfun. */
(How do you feel ?) Well, it's not ......

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);/* declares a pointer variable to point to the same parameter, return value function. */
Int main (INT argc, char * argv [])
{
Myfun (10);/* call the myfun (10) 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. */
Return 0;
}
Void myfun (int x) // A myfun function is defined here.
{
Printf ("% d \ n", X );
}
I changed (★) Line (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 the myfun (10) 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 the myfun (10) 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 also use this call format for function name myfun */
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! Based on previous knowledge and experience, I think "New Discoveries" in this article will certainly come to the following conclusions:
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) But if the function name is called like (* myfun) (10), it is inconvenient to write and read. Therefore, the designers of C language can design and allow myfun (10) to be called in this form (this is much more convenient and the function form in mathematics is the same, isn't it ?).
3) for uniformity, the funp function pointer variable can also be called in the form of funp (10.
4) when the value is assigned, 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);/* 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;/* is equivalent to "int * PX = & X. The pint type is actually int * type */
* PX = 10;/* PX is the 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 declaration here can also be written as void myfun (INT )*/
Typedef void (* funtype) (INT );/*(★) To define a function pointer type */
Funtype funp;/* then 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;
Return 0;
}
Void myfun (int x)
{
Printf ("% d \ n", X );
}
View (★) Line:
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 phrase "funtype funp;" is like "Pint PX;" to declare a funp variable.

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;
/*...*/

6. function pointer as a function parameter

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, consistent with ① Function Type */
Void callmyfun (funtype FP, int X );
Int main (INT argc, char * argv [])
{
Callmyfun (myfun1, 10);/* 5. Call three different functions through the callmyfun function */
Callmyfun (myfun2, 20 );
Callmyfun (myfun3, 30 );
}
Void callmyfun (funtype FP, int X)/* ③. the type of the parameter FP is funtype. */
{
FP (x);/* 4. 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 function myfun1: % d \ n", X );
}
Void myfun2 (int x)
{
Printf ("output in function myfun2: % d \ n", X );
}
Void myfun3 (int x)
{
Printf ("output in function myfun3: % d \ n", X );
}
Output result: For a brief analysis, see the comments I have written. You can analyze it by yourself in the order of my comments ① ③ ④.

C Function Type and function pointer usage

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.