Game Development Community

how to convert String to char* ?

by Ahsan Muzaheed · in Torque 3D Professional · 06/27/2013 (12:48 am) · 6 replies

here is my declarations:

String previousMat=getMapEntry(textureName);
char* t;

problematic lines:
strcpy(t,previousMat);//no error.but crash happens for access violation reading
//char* newMat=(char*)previousMat;//error

there are functions to convert char to string.(sourcecoreutilstr.h)

is there any function to convert string to char?

About the author

Torque 3D enthusiastic since 2010.Have been working in several T3D projects besides of Unreal Engine 4 and Unity 3D. NEED a hand with your project? SHoot me a mail. http://www.garagegames.com/community/forums /viewthread/138437/


#1
06/27/2013 (4:18 am)

const char* blah = StringTable->insert("Blah");
String s = String(blah);

Con::warn(s.c_str()); (.c_str converts it back to a const char *)
#2
06/27/2013 (6:53 am)
To expand on what Vince said: strpcy is intended for C-strings only.

You're using it on a container which also contains length, hash etc. That won't work. Change this:

strcpy(t,previousMat);//no error.but crash happens for access violation reading  
//char* newMat=(char*)previousMat;//error

To this:

newMat = previousMat.c_str ();

And you're done! You won't need your "t" char variable.
#3
06/28/2013 (3:48 am)
thanks for the answers.
very much handy.

although what i was intended to do is concatenate three strings together.
that is why trying to use strCat() after using strcpy() .

but found these :
String& operator+=(StringChar);
   String& operator=(const StringChar*);
   String& operator+=(const StringChar*);


so fix was easy for me:
String z=previousMat+" "+newMat;
 dStrcpy(newMat ,z);

again thanks for yours time.
#4
06/28/2013 (5:00 am)
Glad you got it working!
#5
06/28/2013 (5:22 am)
Shouldn't that be
dStrpy(newMat, z.c_str());
The current line may work but I think you need the method call to be technically correct.
#6
06/28/2013 (6:18 pm)
I would generally agree until I looked at the String code and saw that the value is typedef'd to const char * anyways, so he should be fine without that.