C/C++ Basics--function pointer
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));
}
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));
}