Page 1 of 1

Array question (cpp)

Posted: Fri Feb 09, 2007 3:42 am
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!

Posted: Fri Feb 09, 2007 5:45 am
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

Posted: Fri Feb 09, 2007 6:50 am
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.

Posted: Fri Feb 09, 2007 9:17 am
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:

Posted: Fri Feb 09, 2007 1:16 pm
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.