Array question

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
StuCollier
Posts: 52
Joined: Tue Mar 30, 2004 12:16 pm

Array question

Post by StuCollier »

Can someone explain how I can use Irrlichts "array" to store some strings.

In the example below Im trying to add 2 strings to the array. Whenever I overwrite the "TestText" it changes the ones in the array also....

Does the array only old a pointer to the original string ? so in effect am I pushing a pointer to the var TestText ?

In which case how do I push the contents of the string ?

Code: Select all


irr::core::array<char *> TestArray;
char TestText[] = "POS1";
char TestText2[] = "POS2";


TestArray.push_back(TestText);   // push POS1
strcpy(TestText, TestText2);       // copy POS2 over POS1
TestArray.push_back(TestText);  // push POS2


printf("1 = %s \n", TestArray[0]);   // this says POS2
printf("2 = %s \n", TestArray[1]);  // and this also says POS2

StuC
Freeware games
www.ovine.net
yin nadie
Posts: 43
Joined: Wed Mar 31, 2004 1:03 pm
Location: Seville, Spain
Contact:

Post by yin nadie »

lets see..

Code: Select all

TestArray.push_back(TestText);   // push POS1
strcpy(TestText, TestText2);       // copy POS2 over POS1
TestArray.push_back(TestText [SHOULDN'T IT BE TestText2??] );  // push POS2 
TestText is a pointer to char and it always points to the same memory address.

the char[] type is also a pointer to char. To make it all short, just say you're pushing in the core::array objet the same memory adress twice.

strcpy doesn't change the memory address, It just writes in the memory adress a new set of values.

That about pointers. Now, to store strings irrlicht implements a nice string<T> class. try this:

Code: Select all

irr::core::array<irr::core::string<c8>> TestArray;

TestArray.push_back( irr::core::string<c8>( "POS1" );
TestArray.push_back( irr::core::string<c8>( "POS2" );

printf( "1 = %s\n", TestArray[0].c_str() );
printf( "2 = %s\n", TestArray[1].c_str() );
Isn't it bautiful?
StuCollier
Posts: 52
Joined: Tue Mar 30, 2004 12:16 pm

Post by StuCollier »

perfick !


Thanks a bunch.
StuC
Freeware games
www.ovine.net
StuCollier
Posts: 52
Joined: Tue Mar 30, 2004 12:16 pm

Post by StuCollier »

One more question :)

If I start to use string<T> class is there an easy way to convert between <c8> and <wchar_t> ?


i.e. a stringc = stringw and visa versa..

I am currently using " mbstowcs() " but cant see any way to make it work....
StuC
Freeware games
www.ovine.net
Guest

Post by Guest »

StuCollier wrote: If I start to use string<T> class is there an easy way to convert between <c8> and <wchar_t> ?

i.e. a stringc = stringw and visa versa..
stringc sc8;
stringw sc16=L"Hello StuCollier";
sc8=sc16.c_str();
StuCollier
Posts: 52
Joined: Tue Mar 30, 2004 12:16 pm

Post by StuCollier »

as simple as that :oops: DOH

I'll get to grips with this sooner or later :)
StuC
Freeware games
www.ovine.net
Post Reply