> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Ioannis Vranos
> Sent: Sunday, June 30, 2002 11:34 PM
> To: 'Dev-C++ Mailing List'
> Subject: RE: [Dev-C++] Array of function pointers
>
>
> > -----Original Message-----
> > From: dev...@li...
> > [mailto:dev...@li...] On Behalf
> > Of Abhijit Shylanath
> > Sent: Sunday, June 30, 2002 9:27 PM
> > To: Dev-C++ Mailing List
> > Subject: [Dev-C++] Array of function pointers
> >
> >
> > Sorry for my repeated OT posts, but I don't subscribe to many
> > lists because
> > they have too much of traffic, and poor little me has a
> > dial-up account. Of
> > all the lists I do subscribe to, Dev-C++ seems to have the most
> > knowledgeable (and tolerant) people.
> >
> > Having finished buttering you up, here's my question:
>
>
>
> Always ask. That is why this list is for. Coders talking with
> coders. :)
>
>
> > How do I create an array of function pointers?
> >
> > I declare a function pointer as:
> >
> > void (*fnPtr)(int arg1) = fn;
>
>
> Or better: void (*fnPtr)(int);
>
>
>
>
>
> > I tried making an array like this:
> >
> > void *(*fnPtrs)(int)
>
>
> This is a pointer to a function returning void * and getting an int.
>
>
>
> >
> > Didn't work.
> >
> > void (*fnPtrs)(int)[] = { fn, foo, NULL };
>
>
> void (*fnPtrs[])(int) = { fn, foo, NULL };
>
>
> Or if you cannot think that big, you can make things simpler:
>
>
> typedef void (*p2f)(int);
>
> p2f fnPtrs[]= { fn, foo, NULL };
>
>
>
> > Didn't work either.
> >
> > Ultimately, I got frustrated and made an array of dwords:
> >
> > dword fnPtrs[] = { (dword)fn, (dword)foo, (dword)NULL };
> >
> > This worked, without any warnings. Is this portable and safe?
>
>
> It depends on what dword is, but it doesn't sound safe.
>
>
>
> > Is there any
> > other ANSI way of making an array of function pointers?
> >
> > Thanks for any help.
If you do it in C++, instead of a built in arrray use a vector of
pointers.
An example:
#include <vector>
// ...
using std::vector;
// ...
vector<void (*)(int)>fnPtrs(2, fn);
fnPtrs[1]=foo;
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
|