Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
C / C++ question

 
Post new topic   Reply to topic Printable version
 View previous topic  Help time. Post :: Post RiOT Derelict  View next topic  
Author Message
Bak
?ls -s
0 in


Age:26
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Wed Aug 03, 2005 1:36 am   Post maybe stupid    Post subject: C / C++ question Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List AIM Address
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Wed Aug 03, 2005 1:48 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

const int (*fp[10])(int);

Each function has the form:

int f0 (int) { }
_________________
4,691 irradiated haggis!
Back to top
View users profile Send private message Add User to Ignore List
Mine GO BOOM
Hunch Hunch
What What
Hunch Hunch<br>What What


Age:42
Gender:Gender:Male
Joined: Aug 01 2002
Posts: 3615
Location: Las Vegas
Offline

PostPosted: Wed Aug 03, 2005 1:49 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

Code: Show/Hide
#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;
}

Code: Show/Hide
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
View users profile Send private message Add User to Ignore List Send email
Bak
?ls -s
0 in


Age:26
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Wed Aug 03, 2005 1:57 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

That works.
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Bak
?ls -s
0 in


Age:26
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Wed Aug 03, 2005 2:07 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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:
Code: Show/Hide

int (*f)(int) = 0;
int (*f2)(int) = 0;

const int (*fp[2])(int) =
{
   f,
   f2
};


got it to work though, you need
Code: Show/Hide

int (*const fp[2])(int) =
{
   f,
   f2
};
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Wed Aug 03, 2005 3:03 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

You could also typedef a function pointer, then declare a const array of that type.
Back to top
View users profile Send private message Add User to Ignore List
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Wed Aug 03, 2005 10:36 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

Ekted wrote:
You could also typedef a function pointer, then declare a const array of that type.

Yeah, that is most definitely what I would do. It seems that writing it like that helps overcome per-compiler differences, too.

Code: Show/Hide
   typedef void (__stdcall * template_func)(AnonymousStruct *);
   template_func RPC[256];
   // void (__stdcall * RPC [256])(AnonymousStruct *);

The commented line only works in MSVC. The two uncommented work in MSVC and MinGW.
_________________
This help is informational only. No representation is made or warranty given as to its content. User assumes all risk of use. Cyan~Fire assumes no responsibility for any loss or delay resulting from such use.
Wise men STILL seek Him.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Trash Talk All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 43 page(s) served in previous 5 minutes.

phpBB Created this page in 0.579807 seconds : 31 queries executed (87.9%): GZIP compression disabled