Function pointers

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 :( )
It doesn't look valid to me...

If I were to guess, it is an array of ten pointers to function pointers of type void* (*fp1)(int).

[edit] Do you have any examples of how fp1 is used?
Last edited on
pointer to function returning pointer to *array[10] type.


void *(*(*fp1)(int))[10];

these make it of type void** or *void[10] type.

second pointer from left make it pointer to *void[10].

and third one is the function pointer.

hopefully this is what it meant.
Thanks for the reply guys..... but can anyone write a simple function which this fp1 can point to? I am not able to do so

Please check the code
#include <iostream>

using namespace std;
void ** MyFunc(int k)
{
void ** p = new void *[10] ;

cout << "Inside MyFunc" << endl;

return p;
}
int main(int argc, char* argv[])
{
void *(*(*fp1)(int))[10] = NULL;

fp1 = &MyFunc;
(*fp1)(1);

return 1;
}

ERROR:
error C2440: '=' : cannot convert from 'void **(__cdecl *)(int)' to 'void *(*(__cdecl *)(int))[10]'

this for the line fp1 = &MyFunc;

Can you help me run the code?
your function looks fine..

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
This is a little absurd but I figured it out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// easy way .. typedef

typedef void* 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
  static void* 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.
Last edited on
:o....

damn... thats what i was looking for.. how to declare a function returning *(fun)[10].. never did it coz i can return a **...
hehehe....

but i may not agree on how you have allocated the memory..!!

anyway.. i learned how to make a function return *(fun)[].. :D

good one disch.
Last edited on
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.
haahahaha.. .i know you also didnt liked it and thats why i didnt highlighted what i wrote...
but we can leave it.. what we wanted is solved though..


but i want to point one thing..
as you have typedef it, its already a pointer so you dont have to do this:

ugly *u;
i think this will do..
1
2
3
4
5
6
7
8
ugly    u;//allocated 10 pointers, do you agree?

*(u+0) = (char*)malloc(10);
*(u+1) = (char*)malloc(10);
strcpy(*(u+0),"Hello");
strcpy(*(u+1),"Hello");

printf("%s\n%s\n",*(u+0),*(u+1));


what you say??
as you have typedef it, its already a pointer so you dont have to do this:


The function returns a pointer to a pointer to an array of 10 pointers. So yeah -- you need the extra pointer there.

ugly u;//allocated 10 pointers, do you agree?


Yes -- but that's not what the function returns.

what you say??


You have no chance to survive make your time.
you have put the function part into your pointer..!!!
the function will return and not your data type.. then this will make it **void[10].

You have no chance to survive make your time.


:o


anyway .. forget it..
you have put the function part into your pointer..!!!
the function will return and not your data type.. then this will make it **void[10].


I'm not sure I get what you're asking.

*(void*[10]) <-- what the function returns
void*[10] <-- what 'uglyrettype' is typedef'd as
*(void*[10]) <--- which makes this 'uglyrettype*'

which is why you return 'uglyrettype*' and not 'uglyrettype'

:o


You never heard of Zero Wing / All Your Base?

http://en.wikipedia.org/wiki/All_your_base

Captain: What you say !!
CATS: You have no chance to survive make your time.
ha ha ha ha..!!!


never heard about the game.. though "all your base are belong to us" is a cheat in warcraft..!! hehehehe..
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 :)
Topic archived. No new replies allowed.