I understand that similar questions with answers have been asked before. I already understand the concept of a function pointer and the meaning of its declaration. Following is my sample code of a function's pointer, where I assign the function pointer fp_GetBiggerElement
to point to the function GetBiggerElement
#include <iostream>
typedef int (*fp_GetBiggerElement) (int, int);
// ^ ^ ^
// return type type name arguments
using namespace std;
int GetBiggerElement(int arg1, int arg2)
{
int bigger = arg1 > arg2 ? arg1 : arg2;
return bigger;
}
int main()
{
fp_GetBiggerElement funcPtr = GetBiggerElement;
int valueReturned = funcPtr(10, 20);
cout << valueReturned << endl;
return 0;
}
What I understand is, typedef
means to assing a new definition for a type. Usually, the typedef
statement needs two-parts
(or say two-arguments)
first part is the type which needs a new alias
second part is a new alias name
typedef unsigned int UnsignedInt; // ^ ^ // (original type) (Alias)
for example, in the code above, the first part is unsigned int
and the second part is UnsignedInt
typedef char * CharPtr;
// ^ ^
// (original type) (Alias)
similarly, in the second example above, the first part is char*
and the second part is CharPtr
QUESTION: I am confused with the FORMAT of the typedef
statement for a function pointer. In the statement typedef int(*fp_GetBiggerElement)(int, int);
, the typical two-arguments format for typedef
is not followed then, how is it working?
UPDATE: I am aware that C++11 offers another way of declaring function pointer i.e. (by using
statment). In this question, I am only interested to understand the syntax of typedef
statement for a fucntion pointer.