Complete the C language pointer seventh article

Source: Internet
Author: User
Tags continue printf

function name and function pointer

A number of calls

An example of a common function call:

//自行包含 头文件
void MyFun(int x);    //此处的申明也可写成:void MyFun( int );
int main(int argc, char* argv[])
MyFun(10);     //这里是调用MyFun(10);函数
return 0;
void MyFun(int x)  //这里定义一个MyFun函数
printf ("%d\n",x);

The Myfun function is a function that has no return value, and it does not do anything. The format of this call function you should be very familiar with it! 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).


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. So the function pointer variable should also be stated first? And how is that to be affirmed? In the example above, let me state 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 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:

void MyFun(int x);    //这个申明也可写 成:void MyFun( int );
void (*FunP)(int );   //也可申明成void(*FunP)(int x),但习惯 上一般不这样。
int main(int argc, char* argv[])
MyFun(10);     //这是 直接调用MyFun函数
FunP=&MyFun;  //将MyFun函数的地址赋给FunP变量
(*FunP)(20);     //这是通过函数指针变量FunP来调用MyFun函数的。
void MyFun(int x)  //这里 定义一个MyFun函数

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:

void MyFun(int x);
void (*FunP)(int );    //申明一个用以指向同样参数,返回值函数 的指针变量。
int main(int argc, char* argv[])
MyFun(10);     //这里是 调用MyFun(10);函数
FunP=MyFun;  //将MyFun函数的地址赋给FunP变量
FunP(20);    //这是通过函数指针变量来调用MyFun函数的。
return 0;
void MyFun(int x)  // 这里定义一个MyFun函数

I changed the bold character section (please compare yourself with the previous code).

Run and try, Ah! Succeed as well.



It is possible to assign myfun values to FUNP, so is myfun and FUNP the same data type (that is, the relationship between int and int), rather than the relationship between int and int*? (Is there a little bit of confusion?) )

It seems to be a little contradictory with the previous code, right! So I say!

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!) ):

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