In various c++ code you often see different usage of strings: PWSTR, char*, std::string, CString, etc ...
When is the best time to use PWSTR as compared to any other string type?
a PWSTR would be a wchar_t string pointer. That is a UNICODE (usually UCS2) string with each character taking 16 bits. a char* would be a pointer 8 bits per character. this could be ASCII, ANSI, UTF8, or one of many hundreds of other encodings.
What's different with <string>, <cstring> in C++? The cstring header provides functions for dealing with C-style strings — null-terminated arrays of characters. This includes functions like strlen and strcpy. It's the C++ version of the classic string.
char* is typically used to iterate through a character array, i.e., a C string. It is rarely used as a pointer to a single char, unlike how other pointers are typically used. C++ has newer constructs for strings that typically should be used.
char is a primitive data type whereas String is a class in java. char represents a single character whereas String can have zero or more characters. So String is an array of chars.
a PWSTR would be a wchar_t
string pointer. That is a UNICODE (usually UCS2) string with each character taking 16 bits.
a char*
would be a pointer 8 bits per character. this could be ASCII, ANSI, UTF8, or one of many hundreds of other encodings. Although you only need to worry about encodings if you need the string to hold languages other than English or special symbols.
In general, The Windows API is all UNICODE internally so most Windows programmers use wchar strings. But std::string
and CString
can both be UNICODE if the right symbols are #defined
, so your choice between PWSTR
, std::string
and CString
will be a matter of preference or the convention of the codebase you work with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With