Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++: How to declare constant array of pointers of TCHAR

C++: How to declare constant array of pointers of TCHAR

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structurestutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    akirilov
    wrote on last edited by
    #1

    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 ...

    T 1 Reply Last reply
    0
    • A akirilov

      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 ...

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      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]

      A 1 Reply Last reply
      0
      • T toxcct

        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]

        A Offline
        A Offline
        akirilov
        wrote on last edited by
        #3

        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

        T D 2 Replies Last reply
        0
        • A akirilov

          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

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          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 with FOO *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
          #endif

          On 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]

          A 1 Reply Last reply
          0
          • T toxcct

            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 with FOO *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
            #endif

            On 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]

            A Offline
            A Offline
            akirilov
            wrote on last edited by
            #5

            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.

            T 1 Reply Last reply
            0
            • A akirilov

              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.

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              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]

              1 Reply Last reply
              0
              • A akirilov

                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

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                akirilov wrote:

                I don't know the difference of TCHAR* xx and TCHAR *XX

                xx is a different variable than XX. 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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups