Complete C pointer-function name and function pointer

Source: Internet
Author: User
Complete C pointer-function name and function pointer

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 );
In the beginning, we only understood the function of myfun in terms of functionality or mathematics. We know that the name of myfun function represents a function (orCode).
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, good,ProgramIt 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 custom data types, you can define a function pointer type first, 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 ()
{< br> int X;
pint PX = & X; // It is equivalent to int * PX = & X. Pint type is actually int * type
* PX = 10; // PX is the int * type variable
return 0;
}< br> according to the comment, it should be easy to understand! (Although you may rarely use this definition, it will often be seen later when learning Win32 programming .)
let's take a look at the definition and usage of the function pointer type: (Please compare with the above !)
// self-contained header file
void myfun (int x); // The statement can also be written as void myfun (INT );
typedef void (* funtype) (INT); // 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 parameter of a function. 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 three functions myfun1, myfun2, and myfun3 by using different function pointer values (Note: The definition formats of these three functions should be the same ).
implementation: the code is as follows:
// self-contained header file
void myfun1 (int x);
void myfun2 (int x );
void myfun3 (int x);
typedef void (* funtype) (INT); // ②. define a function pointer type funtype, and 1 to
void callmyfun (funtype FP, int X);

int main (INT argc, char * argv [])
{< br> callmyfun (myfun1, 10); // ⑤. call three different functions using callmyfun functions
callmyfun (myfun2, 20);
callmyfun (myfun3, 30 );
}< br> void callmyfun (funtype FP, int X) // ③. the type of the parameter FP is funtype.
{< br> FP (x); // ④. use the FP pointer to execute the passed function. Note that the function referred to by FP has a parameter
}< br> void myfun1 (int x) // ①. this is a function with a parameter. The following two functions are also the same
{< br> printf ("output in function myfun1: % d \ n", X );
}< br> void myfun2 (int x)
{< br> printf ("output in myfun2: % d \ n", X );
}< br> void myfun3 (int x)
{< br> printf ("output in myfun3: % d \ n", X );
}< br> output result: omitted

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.