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

Source: Internet
Author: User
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. )

The 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 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:

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.