character array to string

Started by
3 comments, last by MattCarpenter 21 years, 8 months ago
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
//att
Advertisement
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
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 ().
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!
Johan
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#elsetypedef wchar_t TCHAR#endif 


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

This topic is closed to new replies.

Advertisement