Archived

This topic is now archived and is closed to further replies.

MattCarpenter

character array to string

Recommended Posts

This is probablly a real stupid question, but How do I convert a character array (char whatever[255]) into a string (like an LPCSTR or a char *whatever_string)? Thanks :D /\/\att

Share this post


Link to post
Share on other sites
char *whatever_string = whatever;

Using just the name of an array gives you the address of the first element. Just remember that whatever_string only points to whatever, it doesn''t actually contain the data, and if something happens to whatever, whatever_string will potentially point to garbage.

I never use LPCSTR I assume its basically the same as a char*, but I really don''t know.



Marc Hanson
Programmer - Alter Echo
Outrage Games

Share this post


Link to post
Share on other sites
It already is a string, at least in that format.


  
char szName [] = "Melraidin";

printf ( "%s\n", szName );


is the same as:


  
char *szName = "Melraidin";

printf ( "%s\n", szName );


Although if possible you may want to consider std::string; it''s much easier to use in most cases, and can be easily converted to a char array by using string.c_str ().

Share this post


Link to post
Share on other sites
I beleive that ''LPCSTR'' is the same thing as a ''const char*''.
It''s the type used by many functions taking a string argument. the ''const'' means that they can''t change it!

Share this post


Link to post
Share on other sites
it''s actually LPCTSTR

LP = Long pointer. A hold over from 16 bit Windows days
C = Const
TSTR = an array of TCHARs, basically TCHAR*

TCHAR =

#if defined(_UNICODE)
typedef char TCHAR
#else
typedef wchar_t TCHAR
#endif


basically, you use TSTR and CTSTR if you want your code to be portable between ASCII and UNICODE builds.

Share this post


Link to post
Share on other sites