Saturday, August 18, 2007

Function returning Pointer to array of function pointers

Function returning Pointer to array of function pointers

int (*(*FRAPtr())[5])(int);

We will discuss this .

as usual start from FRAPtr
So FRAPtr is a
Go to right we get Function ,
Function ...
now go to left , we find * ,nothing but returning a pointer to
So we get
returning pointer to ...
now the parentheses over.
So go right, we get an array of 5
to an array of 5 ...

Now go left , we find "*" which in nothing but pointers to
pointers to
Now the parentheses over;
Now go right ,we find (), which is nothing but function taking one integer argument
function taking one integer argument
Now go left whar we get , an integer , so it is returning int.
returning int

Whatever we have collected as of now , just put together .....

FRAPtr is a function, which returns a pointer to an array of 5, whose elements are function pointers, pointing to functions which takes an integer argument and returns an integer.


just relax ,

Example , illustrating the above declaration.

#include
int Func1(int x)
{
return x+x;
}
int Func2(int y)
{
return y+y;
}

int (*FuncPtrArray[5])(int);//it is global


int (*(*FuncRetArrayPtr())[5])(int)
{
FuncPtrArray[0] = Func1;
FuncPtrArray[1] = Func2;
return &FuncPtrArray;
}
int main()
{
cout << "Calling Func1 ..." << FuncRetArrayPtr()[0][0](10) << endl;
cout << "Calling Func2 ..." << (*FuncRetArrayPtr())[1](2) << endl;
return 0;
}

If you have any doubt , then please do mail me
hara.sahu@gmail.com
Cheer,
Sahu

No comments: