C++: How to declare constant array of pointers of TCHAR
-
I need a constant array with pointers to two words - OK, CANCEL. I tried: const TCHAR Texts[2][7] = {L"OK", L"Cancel"}; It works fine, but I want to remove [7] and put [] instead, so when I change the text, I don't need to calculate the largest possible size. Also I want to have pointers to this words ...
-
I need a constant array with pointers to two words - OK, CANCEL. I tried: const TCHAR Texts[2][7] = {L"OK", L"Cancel"}; It works fine, but I want to remove [7] and put [] instead, so when I change the text, I don't need to calculate the largest possible size. Also I want to have pointers to this words ...
first of all, you're making a type mistake. if you're prepending the strings with a
**L**
, then you must use WCHAR. if you still want to continue with TCHAR, then L is not what you're after. You must use**_T()**
around the string. this being said, why don't you just do this :const TCHAR* Texts[2] = { _T("OK"), _T("Cancel") };
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
first of all, you're making a type mistake. if you're prepending the strings with a
**L**
, then you must use WCHAR. if you still want to continue with TCHAR, then L is not what you're after. You must use**_T()**
around the string. this being said, why don't you just do this :const TCHAR* Texts[2] = { _T("OK"), _T("Cancel") };
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Than you very much!!! It worked !!! Actually I'm compiling for WM5.0, and TCHAR is WCHAR by default, so L is ok. Is there a place when I can read for the foundation of C++?? I don't know the difference of TCHAR* xx and TCHAR *XX
akirilov wrote:
I don't know the difference of TCHAR* xx and TCHAR *XX
actually, this is just a C++ language stuff.
FOO* XX
has absolutely no difference withFOO *XX
. it's just a matter of taste when declaring a variable... TCHAR however is part of the MFC, and is declared like the following IIRC:#if defined(UNICODE) || defined(_UNICODE)
#define TCHAR WCHAR
#else
#define TCHAR char
#endifOn the same, _T() is defined like this:
#if defined(UNICODE) || defined(_UNICODE)
#define _T(x) L##x
#else
#define x
#endif[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
akirilov wrote:
I don't know the difference of TCHAR* xx and TCHAR *XX
actually, this is just a C++ language stuff.
FOO* XX
has absolutely no difference withFOO *XX
. it's just a matter of taste when declaring a variable... TCHAR however is part of the MFC, and is declared like the following IIRC:#if defined(UNICODE) || defined(_UNICODE)
#define TCHAR WCHAR
#else
#define TCHAR char
#endifOn the same, _T() is defined like this:
#if defined(UNICODE) || defined(_UNICODE)
#define _T(x) L##x
#else
#define x
#endif[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
I know that _T(xx) is better (it works for ANSI and Unicode) then Lxx , but when I need a constant string it is easier to put just L (because for WM you almost always work with Unicode). P.S. If you know a place on Internet, where I can learn some basic C++ conception this will be great.
-
I know that _T(xx) is better (it works for ANSI and Unicode) then Lxx , but when I need a constant string it is easier to put just L (because for WM you almost always work with Unicode). P.S. If you know a place on Internet, where I can learn some basic C++ conception this will be great.
It is not a question of "one is better than the other". you MUST use _T() with TCHAR and L with WCHAR. the point that UNICODE is defined is not for "WM" at all. it is defined in your project settings, and some might want to build a non unicode version. if your code is well written then, only switching the parameter in the project settings will do. otherwise, you'd have to change every line of code using L to remove it, so that the ANSI build can perform successfully. so be independant of that, and use _T() ;)
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Than you very much!!! It worked !!! Actually I'm compiling for WM5.0, and TCHAR is WCHAR by default, so L is ok. Is there a place when I can read for the foundation of C++?? I don't know the difference of TCHAR* xx and TCHAR *XX
akirilov wrote:
I don't know the difference of TCHAR* xx and TCHAR *XX
xx
is a different variable thanXX
. Whether you put the space before or after is irrelevant, as the compiler sees tokens rather than spaces."Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch