Hi can somebody explain me what this function pointer means...
void *(*(*fp1)(int))[10];
As I understand this means its is a pointer to a function which takes int as an argument and returns an array of 10 void pointers.
Is it correct?
And can somebody write a simple function of this type? I mean which takes int as an argument and returns array of 10 pointers? (I am not able to do so :( )
but the problem is what you have written.. i also tried on the same lines and getting the same error.. how to declare a function of type void* (*(*)(int)[10]
if we cast it.. then the problem can be solved and the function pointer will run fine too.. but i dont know if thats the correct way..
i mean this:
typedef void *(*(*FP1)(int))[10];
fp1 = (FP1)MyFunc;
fp1(10);
by the way from where you have taken this thing??? :D
// easy way .. typedef
typedefvoid* uglyrettype[10];
uglyrettype* func_a(int v) { /* ... */ }
// harder way, no typedef
void* (*func_b(int k))[10] { /* ... */ }
// functions return a pointer to a pointer to an array of 10 void pointers
// ie:
uglyrettype* func_a(int v)
{
uglyrettype* ret = (uglyrettype*)malloc(sizeof(uglyrettype));
// *ret is 10 void pointers:
int butt, booty;
(*ret)[0] = &butt;
(*ret)[1] = &booty;
// etc
return ret;
// alternatively -- can do something like this
staticvoid* r2[10];
return &r2;
}
///-----------------------------
//
void *(*(*fp1)(int))[10];
fp1 = &func_a; // works
I tried looking for ways to allocate the return value with new, but couldn't figure it out, so I resorted to malloc. void* x[10] is not the same as void** x for purposes of new, apparently, so it gets tricky.
EDIT:
Hrm... apparently I just tried this:
1 2
uglyrettype* r1 = new uglyrettype; // does not compile
uglyrettype* r2 = new uglyrettype[10]; // does compile
I don't know if that r2 is what you'd want though?
In any event -- whoever came up with this function pointer needs to be shot.
but i may not agree on how you have allocated the memory..!!
I don't like it either. But like I say, I couldn't find a way to make new work. uglyrettype* r2 = new uglyrettype[10]; compiled, but I think it allocates 10x more memory than you'd need.
Thanks guys. :)... i understand this was a very difficult one... and the culprit is none other than Bruce Ackels... in his Thinking in C++... :) ..... Hope u will help me the same way in future too :)