1. Function pointers in the C language

Source: Internet
Author: User

A common function call

1 voidMyfun (intx);//the declaration here can also be written as: void myfun (int);2 3     intMainintargcChar*argv[])4     {5Myfun (Ten);//here is the call to Myfun (10);6 7           return 0;8     }9 Ten     voidMyfun (intX//This defines a myfun function One     { Aprintf ("%d\n ", x); -}



This myfun function is a function without a return value, and it does not accomplish anything. 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?


Declaration of two 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 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 the function pointer variable should also be declared first? How is that to be stated? For the example above, let me 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);

If a typedef is used to define a type of function pointer of this type

typedef void (*func_t) (int);

func_t PFunc;

Pfunc=myfunc;

(*pfunc) (10);


You see, the declaration format of the entire function pointer variable is the same as the declaration of the function myfun, except--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.) )

Three call 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:
Self-Contained header file

1     voidMyfun (intx);//This statement can also be written as: void myfun (int);2     void(*FUNP) (int);//can also be declared void (*FUNP) (int x), but generally not. 3 4     intMainintargcChar*argv[])5     {6Myfun (Ten);//This is a direct call to the Myfun function7funp=&myfun;//assign the address of the Myfun function to the FUNP variable8(*FUNP) ( -);//This is called by the function pointer variable FUNP to call the Myfun function. 9     }Ten  One     voidMyfun (intX//This defines a myfun function A     { -printf ("%d\n ", x); -}



See the code and comments in the bold section.
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 with Funp=&myfun.
(How do you feel?) )
Oh, not actually--

Four other writing formats for calling functions
function pointers can also be used as follows to accomplish the same thing:
Self-Contained header file

1    voidMyfun (intx);2     void(*FUNP) (int);//declares a pointer variable that is used to point to the same parameter, the return value function. 3 4     intMainintargcChar*argv[])5     {6Myfun (Ten);//here is the call to Myfun (10);7Funp=myfun;//assign the address of the Myfun function to the FUNP variable8FUNP ( -);//This is called by the function pointer variable to call the Myfun function. 9 Ten           return 0; One     } A  -     voidMyfun (intX//This defines a myfun function -     { theprintf ("%d\n ", x); -}



I changed the boldface section (please compare it 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 Myfun (10);
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 Myfun (10);
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! If I were "Sherlock Holmes", according to the knowledge and experience of the past to infer the "new discovery", it 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 language 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 the value is assigned, it can be 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--
Add a point: 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.

Five defines 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 the function pointer variable.
Let me give you an example of a custom data type.

1typedefint* PINT;//a pint alias was defined for the int* type2     intMain ()3     {4       intx;5PINT px=&x;//is equivalent to the int * px=&x;. The pint type is actually an int * type6*px=Ten;//px is a variable of type int*7       return 0;8}



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 take a look at the definition and use of the function pointer type: (please check with the control!) )
Self-Contained header file

1     voidMyfun (intx);//the declaration here can also be written as: void myfun (int);2typedefvoid(*funtype) (int);//This simply defines a function pointer type3Funtype FUNP;//then use the Funtype type to declare the global FUNP variable4 5     intMainintargcChar*argv[])6     {7     //Funtype FUNP;//The function pointer variable can also be partial, so please declare it here. 8Myfun (Ten);9funp=&Myfun;Ten(*FUNP) ( -); One  A           return 0; -     } -  the     voidMyfun (intx) -     { -printf ("%d\n ", x); -}



Look at the blackbody section:
First, in Void (*funtype) (int); Before adding a typedef. This onlyis to define 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;
......

Six 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.
Give you an example:
Requirements: I want to design a callmyfun function, this function can be called by the parameters of the function pointer values are different to call MyFun1, MYFUN2, MyFun3 three functions (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); ②. Define 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 via 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);//④. Performs the passed in function via the FP pointer, noting that the function referred to in the FP is 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: (Look at the notes I wrote.) You can analyze your own ①②③④⑤ in the order of my comments. )

The above sections are reproduced in the Netizen described. The original address is: http://blog.pfan.cn/whyhappy/6030.html

Seven address jump
void (*reset) (void) = (void (*) (void)) 0.

void (*reset) (void) is the function pointer definition, (void (*) (void)) 0 is a coercion type conversion operation, and the value "0" is cast to the function pointer address "0".

By calling the Reset () function, the program jumps to the "0" address at the execution of the program. In some other advanced microcontroller bootloader, such as Nboot, UBoot, eboot, often through the bootloader to download the program, and then through the function pointer to the address to execute the program.

1 void (*theuboot) (void);
。。。。
Theuboot = (void (*) (void)) (0x30700000);
Theuboot ();
。。。。。

2 (* (void (*) (void)) (0x30700000)) ();

Force type conversions, convert an absolute address to a function pointer, and call this function to jump to the absolute address mentioned earlier.
Translation into a compendium is:
MOV r0,0x30700000;
MOV pc,r0

for (* (void (*) (void))) (0x30700000)) ();
You can understand that.

First (void (*) (void)) is a forced-type conversion, which forces the unsigned integer of the following 0x30700000 into a function pointer, which points to a function entry parameter of void and the return value is void.   If you understand this step, then set (void (*) (void)) (0x30700000) is the FP, then the above expression can be simplified to (*FP) (); OK, so it's clear that we'll refer to the converted function pointer above (that is, the function that calls the function pointer).

1. Function pointers in the C language

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.