您的位置:首页 > 编程语言 > C语言/C++

C/C++ Basics--function pointer

2008-04-08 16:03 253 查看
1.basic concepts

function pointer in C/C++ is just like delegate in C#. we can use function pointer to point to a specific function, and then use function pointer to invoke the specified function.

eg.

int max(int a,int b)

{

return a>b?a:b;

}

void main()

{

int (*p)(int,int); //declare a function pointer

p=max;//the function point must has the same return type and parameter type with specified function.

printf("max(2,3) is %d",p(2,3));

}

//the result is 3;

also , we can use typedef to define a function pointer type to simplify the programming.

eg.

#typedef int (*MyFunPointer)(int a,int b);

//we define a function pointer type which has int return type and has two int parameters.

void main()

{

MyFunPointer p;

p=max;

printf("max(2,3) is %d",p(2,3));

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: