Array question (cpp)

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
LwSiX
Posts: 22
Joined: Wed Jan 17, 2007 2:24 pm

Array question (cpp)

Post by LwSiX »

Hi there,

Is it possible to use a char * as an index for an array? For example:

Code: Select all

 int array["test"] = 5
I used to do this with PHP, but c++ just isn't having this!
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

No. Regular arrays are only indexable with integral values. If you want to index by something else, use a map [std::map, or the new core::map].

Travis
AaronA
Posts: 55
Joined: Tue Sep 12, 2006 1:31 am

Post by AaronA »

Code: Select all

#define TEST_INDEX 0
#define BLAH_INDEX 1
#define somethingElse 2
#define Text 3

int numIndex[Text] = 43;
Its kinda lame, and probably not recommended, but it works.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Don't use defines, and even more important: Don't use lower case characters in defined strings. Guess what happens with your code if you later write 'char* Text = "x"' :lol:
LwSiX
Posts: 22
Joined: Wed Jan 17, 2007 2:24 pm

Post by LwSiX »

vitek wrote:No. Regular arrays are only indexable with integral values. If you want to index by something else, use a map [std::map, or the new core::map].

Travis
Thanks, thats exactly what I'm looking for.
Post Reply