Author |
Message |
Bak ?ls -s 0 in

Age:26 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Wed Aug 03, 2005 1:36 am Post maybe stupid Post subject: C / C++ question |
 |
|
|
|
How would one declare a const array of function pointers? I played with this thing for 10 minutes and couldn't get it compiling. Had to settle for making a wrapper struct for the function pointer, then making an array of structs.
I guess a typedef would work too... but seems like it shouldn't be necessary. _________________ SubSpace Discretion: A Third Generation SubSpace Client |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Wed Aug 03, 2005 1:48 am Post maybe stupid Post subject: |
 |
|
|
|
const int (*fp[10])(int);
Each function has the form:
int f0 (int) { } _________________ 4,691 irradiated haggis! |
|
Back to top |
|
 |
Mine GO BOOM Hunch Hunch What What

Age:42 Gender: Joined: Aug 01 2002 Posts: 3615 Location: Las Vegas Offline
|
Posted: Wed Aug 03, 2005 1:49 am Post maybe stupid Post subject: |
 |
|
|
|
#include <stdio.h>
int main()
{
void *fList[2];
fList[0] = printf;
fList[1] = main;
printf("Location of printf: %p\n", printf);
printf("Location of main: %p\n", main);
printf("Location of fList[0]: %p\n", fList[0]);
printf("Location of fList[1]: %p\n", fList[1]);
return 0;
} |
Location of printf: 0x80482a8
Location of main: 0x8048384
Location of fList[0]: 0x80482a8
Location of fList[1]: 0x8048384
|
Worked fine for me. Requires you to typecast it correctly when you use it though. If all the functions are the same style, use Ekted's method. |
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:26 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Wed Aug 03, 2005 1:57 am Post maybe stupid Post subject: |
 |
|
|
|
That works. |
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:26 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Wed Aug 03, 2005 2:07 am Post maybe stupid Post subject: |
 |
|
|
|
Looks like I spoke too soon.
when you do const int (*fp[10])(int); it thinks the function is returning a const int, rather than the array is a const
For example this won't compile:
int (*f)(int) = 0;
int (*f2)(int) = 0;
const int (*fp[2])(int) =
{
f,
f2
};
|
got it to work though, you need
int (*const fp[2])(int) =
{
f,
f2
};
|
|
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Wed Aug 03, 2005 3:03 am Post maybe stupid Post subject: |
 |
|
|
|
You could also typedef a function pointer, then declare a const array of that type. |
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
|
Back to top |
|
 |
|