4

Can someone explain me the difference and the relationship between the char * and CString?... Thanks.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
NULL_USER
  • 125
  • 1
  • 2
  • 8

7 Answers7

3

There are few important differences.

char * is a pointer to char. Generally you can't say if it a single char, or a beginning of a string, and what is the length. All those things are dictated by program logic and some conventions, i.e. standard C functions, like to use const char * as inputs. You need to manage memory allocated for strings manually.

CString is a macro. Depending on your program compilation options, it can be defined to either the CStringA or CStringW class. There are differences and similarities.

The difference is that CStringAoperates with non-Unicode data (similar to char*), and CStringW is a Unicode string (similar to wchar_t*).

Both classes, however, are equivalent in the aspect of string manipulation and storage management. They are closer to the standard C++ std::string and std::wstring classes.

Apart from that, both CStringA and CStringW provide the capability to convert strings to and from Unicode form.

Valeri Atamaniouk
  • 5,125
  • 2
  • 16
  • 18
  • CStringA/CStringW and std::string/std::wstring are quite unlike eachother. The only thing they have in common is they're both C++ classes manipulating strings. This difference is quite obvious when trying to make ATL/WTL user interface code using CString play nicely with backend code using std::string/wstring and standard containers. – Ed Rowlett-Barbu Mar 13 '13 at 10:36
  • @Zadirion Both classes provide a member function that returns a pointer to a C-style string, which makes interop trivial. Differences between their implementation are irrelevant as God intended them to be. When considering the use of a class, all one should care about is the interface. – Cody Gray - on strike Mar 13 '13 at 10:39
  • @CodyGray of course, converting between them is not rocket science. But lack of implicit conversion and the fact that CString does not model the container concept really makes it an ugly to use thing, which one would rather avoid if it wasn't needed by the UI framework. On the upside though, CString is a bit more lightweight, and it also has the useful Format method which you can only get through boost::format if you like writing standard code. – Ed Rowlett-Barbu Mar 13 '13 at 10:42
2

a CString will be an array of char and a char* will be a pointer into the array of char with which you can iterate the characters of the string.

Actually from MSDN:

CString is based on the TCHAR data type. If the symbol _UNICODE is defined for your program, TCHAR is defined as type wchar_t, a 16-bit character type; otherwise, it is defined as char, the normal 8-bit character type. Under Unicode, then, CString objects are composed of 16-bit characters. Without Unicode, they are composed of 8-bit char type.

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
1

CString is a sequence of TCHAR-s rather then char*. The main difference is that if UNICODE is defined CString will be sequence of wchar. Actually depending on that macro CString will be tpyedef -ed either to CStringA or CStringW. Another major difference is that CString is a class while char* is simply a pointer to character.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
1

Depending on the type of TCHAR, CString can be either CStringA or CStringW.

That said, CString is a wrapper over an array of chars, that enables you to easily treat that array of chars as a string, and operate on it in manners relevant to the string type.

For the relationship between them, here is something that illustrates it easily. You can convert between char * and CString like this:

CString str = "abc"; // const char[3] or char * to CString

and

const char * p = str.Get() // CString to const char *

Ed Rowlett-Barbu
  • 1,611
  • 10
  • 27
1

A CString is a class and provides lots of functionalities that a char * doesnt. A char * is just a pointer to char or chars array.

A CString contains a buffer that is roughtly the same as a char * : LPTSTR GetBuffer( int nMinBufLength );

For the difference between LPTSTR and char * go here and here

Community
  • 1
  • 1
Laurent
  • 812
  • 5
  • 15
1

CString is a class packed with different functionalities.. MSDN

char * is just a regular c++ data type.

CString is used mostly in MFC applications.

Aruna Karunarathna
  • 981
  • 2
  • 23
  • 55
0

CString is a wrapper class around a char* to provide some useful additional functions and to hide the memory allocation/deallocation from the user. There is not much difference in performance terms so if you are using MFC classes, you might as well use a CString.

Lukos
  • 1,826
  • 1
  • 15
  • 29