C-language function pointers

Source: Internet
Author: User

Second, the usual function call

An example of a common function call:
/* Include 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); /* Here is the call to the Myfun (10) function */
return (0);
}
void myfun (int x)/* Define a myfun function here */
{
printf ("%d\n", X);
}
This myfun function is a function that has no return value, and it does not "do" something. You should be familiar with the format of calling functions. Look at the writing format of the Myfun function called in the main function:
Myfun (10);
We start with a functional or mathematical understanding of the Myfun function, knowing that myfun function name represents a feature (or a piece of code). Until--when you learn the concept of function pointers. I had to wonder: what was the name of the function?

(Don't think it's a meaningless thing to do!) Oh, continue to look down you will know. )

Ii. 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 also stored in a function pointer variable. In this way, I can invoke the function pointed to by this function pointer variable.
In a C-series language, any variable is always declared before it can be used. So, should the function pointer variables be declared first? How is that to be declared? For the example above, let me declare a function pointer variable FUNP that can point to the Myfun function. Here's how to declare a FUNP variable:
void (*FUNP) (int); /* can also be written as void (*FUNP) (int x) */
You see, the declaration format of the entire function pointer variable is the same as the declaration of the function myfun, except that--we change the myfun to "(*FUNP)", so we have a pointer funp that can point to the Myfun function. (Of course, this FUNP pointer variable can also point to all other functions that have the same parameters and return values.) )

Third, call the function by function pointer variable

With the FUNP pointer variable, we can assign a value to Myfun and then invoke the Myfun function through FUNP. See how I can invoke the Myfun function by FUNP pointer variable:
/* Include 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 generally not in practice. */
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 called by the function pointer variable FUNP to call the Myfun function. */
}
void myfun (int x)/* Define a myfun function here */
{
printf ("%d\n", X);
}
Please look at the code and comments on the line (★). Run to see. Well, yes, the program works very well. Oh, my feeling is that the type relationship between Myfun and FUNP is similar to the relationship between int and int *. The function myfun appears to be a variable (or constant) such as int, and FUNP is a pointer variable like an int *.
int i,*pi;
PI = &i; /* compared to FUNP = &myfun. */
(How do you feel?) Oh, it is not ...

Iv. Other writing formats for calling functions

function pointers can also be used as follows to accomplish the same thing:
/* Include header files */
void myfun (int x);
void (*FUNP) (int);/* Declares a pointer variable that points to the same parameter, which returns a value function. */
int main (int argc, char* argv[])
{
Myfun (10); /* Here is the call to the Myfun (10) function */
FUNP = Myfun; /* Assign the address of the Myfun function to the FUNP variable */
FUNP (20); /* (★) This is called by the function pointer variable to call the Myfun function. */
return 0;
}
void myfun (int x)//define a myfun function here
{
printf ("%d\n", X);
}
I changed (★) line (please compare yourself with the previous code). Run try, Ah! Success as well. Hey?
FUNP = Myfun;
Can you assign the Myfun value to FUNP so that Myfun is the same data type as FUNP (that is, the relationship between int and int), rather than the relationship between int and int*? (Is there a little bit of confusion?) Seems to be a bit contradictory to the previous code, right? So I said!

Please allow me to not explain to you, continue to look at the following several situations (these can be correctly run code yo!) ):
Code Three:
int main (int argc, char* argv[])
{
Myfun (10); /* Here is the call to the Myfun (10) function */
FUNP = &MyFun; /* Assign the address of the Myfun function to the FUNP variable */
FUNP (20); /* This is called by the function pointer variable to call the Myfun function. */
return 0;
}

Code four:
int main (int argc, char* argv[])
{
Myfun (10); /* Here is the call to the Myfun (10) function */
FUNP = Myfun; /* Assign the address of the Myfun function to the FUNP variable */
(*FUNP) (20); /* This is called by the function pointer variable to call the Myfun function. */
return 0;
}
It really can be this way oh! Wow It's going to faint! ) and now! Look--
int main (int argc, char* argv[])
{
(*myfun) (10); /* See, the function name Myfun can also have such a call format */
return 0;
}
You may see it for the first time: function name invocation can also be written like this! (It's just that we don't usually write that way.) So what does that mean?

Oh! Based on previous knowledge and experience to infer the "new discovery" of this article, I think even "Sherlock Holmes" will inevitably analyze and deduce the following conclusions:
1) In fact, the function name of Myfun is the same as the FUNP function pointer, that is, the function pointer. 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 (*myfun) (10) then it is inconvenient and unaccustomed to write and read. So the designers of C are designed and allowed to myfun (10) This form of invocation (which is much more convenient and similar to the form of a function in mathematics, right?). )。
3) For the sake of unification, the FUNP function pointer variable can also be called in the form of FUNP (10).
4) When assigning a value, you can FUNP = &myfun form, or FUNP = Myfun.

The code above the wording, whatever you love how! Please understand this! This is useful for your application of function pointers! Finally--

To add a note, at 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). */
(see note) This is a point to note.

V. Defining a pointer type for a function

Just like a custom data type, we can define a function pointer type first, and then use that type to declare a 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; /* with "int *px=&x;" is equivalent. The pint type is actually an int * type * *
*PX = 10; /* px is a variable of type int* */
return 0;
}
According to the comments, it should not be ugly understand! (although you may rarely use this definition, you will often see it later when you learn Win32 programming.) Let's look at the definition and use of the function pointer type: (please check with the above!) )
/* Include header files */
void myfun (int x); /* The declaration here can also be written as: void myfun (int) */
typedef void (*funtype) (int); /* (★) This simply defines a function pointer type */
Funtype FUNP; /* Then declare the global FUNP variable with the funtype type */
int main (int argc, char* argv[])
{
Funtype FUNP; */* Function pointer variable of course also can be partial, then please declare here. */
Myfun (10);
FUNP = &MyFun;
return 0;
}
void myfun (int x)
{
printf ("%d\n", X);
}
Look (★) Line:
First, a typedef is added before void (*funtype) (int). This simply defines a pointer type named Funtype function instead of a funtype variable.
Then, "Funtype FUNP;" This sentence is like "PINT px;" Declare a FUNP variable as well.

The others are the same. The whole program has done the same thing. The advantages of this approach are:
With the Funtype type, we can declare more than one function pointer variable of the same type with the Funtype type in the same way and conveniently. As follows:
Funtype FunP2;
Funtype FunP3;
/* . . . */

function pointers as parameters of a function

Since a function pointer variable is a variable, it can also be used as a parameter of a function. So, you should also know how a function pointer is passed as an argument to a function.

gives you an example:
Requirements: I'm going to design a callmyfun function that can call the three functions of MyFun1, MYFUN2, MyFun3, respectively, from the function pointer values in the parameters (note: These three functions should have the same definition 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, consistent with the ① function type */
void Callmyfun (Funtype fp,int x);
int main (int argc, char* argv[])
{
Callmyfun (myfun1,10)/*⑤. Call three different functions by callmyfun function */
Callmyfun (MyFun2, 20);
Callmyfun (myfun3,30);
}
void Callmyfun (Funtype fp,int x)/*③. The type of the parameter FP is funtype. */
{
fp (x);/*④. The function passed in via the FP pointer, note that the function referred to by the FP has one 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);
}

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.