Complete the C pointer-function name and function pointer __ function

Source: Internet
Author: User

http://www.programfan.com/blog/article.asp?id=6030

function name and function pointer

a common function call

An example of a common function call:
Self-Contained header file
void myfun (int x); The declaration here can also be written as: void myfun (int);

int main (int argc, char* argv[])
{
Myfun (10); Here is the call to Myfun (10);

return 0;
}

void myfun (int x)///here defines a myfun function
{
printf ("%d\n", X);
}
The Myfun function is a function that has no return value, and it does not do anything. You should be familiar with the format of this call function. Look at the writing format for calling the Myfun function in the main function:
Myfun (10);
We started with the function, or mathematically, to understand myfun, knowing that myfun function names represent a feature (or a piece of code).
Until--
When you learn about the concept of function pointers. I have to think: what is the name of the letter?
(Don't think it's meaningless.) Oh, continue to look down you will know. )

Two declaration of function pointer variable

Just as the memory address of a data variable can be stored in the corresponding pointer variable, the first address of the function is stored in a function pointer variable. In this way, I can use the function pointer variable to invoke the function being pointed to.
In the C-series language, any variable is always declared before it can be used. Then the function pointer variable should also be stated first. And how is that to be affirmed? Take the example above, I declare a function pointer variable FUNP that can point to the Myfun function. Here's how to declare the FUNP variable:
void (*FUNP) (int); Can also be written as void (*FUNP) (int x);
You see, the entire function pointer variable's declaration format is the same as the function Myfun declaration, except that--we change the myfun to (*FUNP), so we have a pointer to the Myfun function FUNP. (Of course, this FUNP pointer variable can also point to all other functions that have the same parameters and return values.) )

three call functions by function pointer variable

With the FUNP pointer variable, we can assign a value to the myfun and then invoke the Myfun function by FUNP. See how I can invoke the Myfun function by FUNP the pointer variable:
Self-Contained header file
void myfun (int x); This statement can also be written as: void myfun (int);
void (*FUNP) (int); It can also be declared void (*FUNP) (int x), but it is not customary in general.

int main (int argc, char* argv[])
{
Myfun (10); This is a direct call to the Myfun function
funp=&myfun; Assign the address of the Myfun function to the FUNP variable
(*FUNP)    (20); This is through the function pointer variable FUNP to invoke the Myfun function.
}

void myfun (int x)///here defines a myfun function
{
printf ("%d\n", X);
}
See the code and comments in the boldface section.
Run to see. Well, yes, the program works very well.
Oh, my feeling is: the type relationship between Myfun and FUNP is similar to the relationship between int and int *. The function myfun seems to be a variable (or constant) such as int, while FUNP is like a pointer variable such as int *.
int i,*pi;
pi=&i; Compared with Funp=&myfun.
(How do you feel?) )
Oh, it's not--

Four other writing formats for calling functions

function pointers can also be used to accomplish the same thing:
Self-Contained header file
void myfun (int x);
void (*FUNP) (int); Declares a pointer variable that is used to point to the same parameter and return a value function.

int main (int argc, char* argv[])
{
Myfun (10); Here is the call to Myfun (10);
Funp=myfun; Assign the address of the Myfun function to the FUNP variable
FUNP (20); This is done by using the function pointer variable to invoke the Myfun function.

return 0;
}

void myfun (int x)///here defines a myfun function
{
printf ("%d\n", X);
}
I changed the bold character section (please compare yourself with the previous code).
Run and try, ah. Succeed as well.
Hey.
Funp=myfun;
It is possible to assign myfun values to FUNP, so that Myfun and FUNP are the same data types (that is, the relationship between int and int), not as int and int*. (There is not a little bit of confusion.) )
It seems to be a little contradictory with the previous code, right. So I said.
Please allow me to temporarily not explain to you, continue to look at the following situations (these can be the right to run the code yo.) ):
Code Three:
int main (int argc, char* argv[])
{
Myfun (10); Here is the call to Myfun (10);
funp=&myfun; Assign the address of the Myfun function to the FUNP variable
FUNP (20); This is done by using the function pointer variable to invoke the Myfun function.

return 0;
}
Code four:
int main (int argc, char* argv[])
{
Myfun (10); Here is the call to Myfun (10);
Funp=myfun; Assign the address of the Myfun function to the FUNP variable
(*FUNP)    (20); This is done by using the function pointer variable to invoke the Myfun function.

return 0;
}
It really can be this way oh.
Wow It's really going to faint. )
And there. Look--
int main (int argc, char* argv[])
{
(*myfun) (10); Look, the function name Myfun can also have such a call format

return 0;
}
You may have seen it for the first time: function name invocation can also be written like this. (It's just that we don't usually write like this.) )
So, what does that say?
Oh. If I were "Sherlock Holmes", based on previous knowledge and experience to infer the "new findings", it will be analyzed and inferred the following conclusions:
1. In fact, myfun function names and FUNP function pointers are the same, that is, are function pointers. The Myfun function name is afunction Pointer Constants, and FUNP is afunction pointer variable, this is their relationship.
2. But if the function name is called (*myfun) (10), then it is inconvenient and unaccustomed to write and read. So the designers of C language will be designed to allow Myfun (10), this form of invocation (so much more convenient and as in the mathematical form of the function, is not it.) )。
3. For the sake of unification, the FUNP function pointer variable can also be invoked in the form of FUNP (10).
4. When assigning value, can funp=&myfun form, also can funp=myfun.
The above code writing, whatever you love how.
Please understand this. This is useful for your application of function pointers.
Finally--
Add a point: in the declaration of a function:
void myfun (int); cannot be written as void (*myfun) (int).
void (*FUNP) (int); cannot be written as a void funp (int).
(see note) This is to be noted.


A program summarizes:

#include <stdio.h>

void myfun (int x);    This statement can also be written as: void myfun (int);

int main (int argc, char* argv[])
{
   myfun);     This is a direct call to the Myfun function
   (&myfun); 
   (*myfun) (a); 
   return 0;
}

void myfun (int x)  //is defined here as a myfun function
{
   printf ("%d\n", x);
}
Results:

10
30
40
Five defines a pointer type for a function:

Just like a custom data type, we can also define a function pointer type and then use that 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 the int * px=&x;. Pint type is actually int * type
*px=10; PX is the int* type of variable
return 0;
}
According to the note, should not be ugly understand it. (although you may rarely use this definition, you will often see it later when you learn Win32 programming.) )
Let's take a look at the definition and use of the function pointer type: (please compare with.) )
Self-Contained header file
void myfun (int x); The declaration here can also be written as: void myfun (int);
typedef void (*funtype) (int); This just defines 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, of course, 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 Bold section:
First, in Void (*funtype) (int); A typedef was added to the front. This simply defines a pointer type named Funtype function, not a funtype variable.
Then, Funtype FUNP; This sentence is like Pint px; declare a FUNP variable.
The other is the same. The whole program completes the same thing.
The advantages of such an approach are:
With the Funtype type, we can use the Funtype type to declare multiple function pointer variables of the same type, just as easily. As follows:
Funtype FunP2;
Funtype FunP3;
......

six function pointers as parameters of a function

Since the function pointer variable is a variable, it can, of course, be used as a parameter to a function. So, you should also know how a function pointer is passed as a parameter to a function.
Give you an example:
Requirements: I want to design a callmyfun function, which can be called MyFun1, MyFun2, MyFun3 by different function pointer values in the parameters (note: These three functions should be defined in the same format).
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); ②. Defines a function pointer type Funtype, with the ① function type one to
void Callmyfun (Funtype fp,int x);

int main (int argc, char* argv[])
{
Callmyfun (myfun1,10); ⑤. Call three different functions through the Callmyfun function
Callmyfun (myfun2,20);
Callmyfun (myfun3,30);
}
void Callmyfun (Funtype fp,int x)//③. The type of parameter FP is funtype.
{
FP (x);//④ the function passed in through the FP pointer, note that the function referred to in FP has a parameter
}
void MyFun1 (int x)//①. This is a function with one parameter, and 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: slightly
Analysis: (see the Notes I wrote.) You can analyze the ①②③④⑤ order of my comments. )

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.