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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. pointer to a constant string

pointer to a constant string

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsharpc++visual-studiocom
5 Posts 4 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.
  • B Offline
    B Offline
    bkelly13
    wrote on last edited by
    #1

    Windows 7, visual Studio 2008, C++

    WCHAR *op_type = NULL;
    if( x )
    op_type = &L"First";
    else
    op_type = &L"Second";

    This solicits the error

    Quote:

    error C2440: '=' : cannot convert from 'const wchar_t (*)[6]' to 'WCHAR *'

    What is my syntax error? I tried the declaration:

    const wchar_t *op_type2 = NULL;

    and received the error:

    Quote:

    error C2440: '=' : cannot convert from 'const wchar_t (*)[6]' to 'const wchar_t *'

    Which seems wrong to me. Why does the pointer care how long the string is? It is but the address of the first character.

    Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

    _ 1 Reply Last reply
    0
    • B bkelly13

      Windows 7, visual Studio 2008, C++

      WCHAR *op_type = NULL;
      if( x )
      op_type = &L"First";
      else
      op_type = &L"Second";

      This solicits the error

      Quote:

      error C2440: '=' : cannot convert from 'const wchar_t (*)[6]' to 'WCHAR *'

      What is my syntax error? I tried the declaration:

      const wchar_t *op_type2 = NULL;

      and received the error:

      Quote:

      error C2440: '=' : cannot convert from 'const wchar_t (*)[6]' to 'const wchar_t *'

      Which seems wrong to me. Why does the pointer care how long the string is? It is but the address of the first character.

      Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      For the first part of your question, you do not need the & sign.

      WCHAR *op_type = NULL;
      if( x )
      op_type = L"First";
      else
      op_type = L"Second";

      For the second part of your question, you should not be getting any errors. It is a valid statement.

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++) (October 2009 - September 2013)

      Polymorphism in C

      B 1 Reply Last reply
      0
      • _ _Superman_

        For the first part of your question, you do not need the & sign.

        WCHAR *op_type = NULL;
        if( x )
        op_type = L"First";
        else
        op_type = L"Second";

        For the second part of your question, you should not be getting any errors. It is a valid statement.

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++) (October 2009 - September 2013)

        Polymorphism in C

        B Offline
        B Offline
        bkelly13
        wrote on last edited by
        #3

        Lets see if I really understand what you told me. The declaration:

        const WCHAR *op_type = NULL;

        was good. The use:

        op_type = &L"Shutdown";

        was incorrect. The declaration of the constant string:

        &L"Shutdown";

        is effectively a WCHAR array. Arrays are always passed by address so adding the & symbol was creating an pointer to a pointer. The error message:

        Quote:

        error C2440: '=' : cannot convert from 'const wchar_t (*)[9]' to 'const WCHAR *'

        was telling me that but I was too wrapped up in reading the [9] as a length rather than an indicator that this is an array and the address is passed rather than the value. Thank you for helping me with this.

        Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

        L 1 Reply Last reply
        0
        • B bkelly13

          Lets see if I really understand what you told me. The declaration:

          const WCHAR *op_type = NULL;

          was good. The use:

          op_type = &L"Shutdown";

          was incorrect. The declaration of the constant string:

          &L"Shutdown";

          is effectively a WCHAR array. Arrays are always passed by address so adding the & symbol was creating an pointer to a pointer. The error message:

          Quote:

          error C2440: '=' : cannot convert from 'const wchar_t (*)[9]' to 'const WCHAR *'

          was telling me that but I was too wrapped up in reading the [9] as a length rather than an indicator that this is an array and the address is passed rather than the value. Thank you for helping me with this.

          Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

          L Offline
          L Offline
          leon de boer
          wrote on last edited by
          #4

          You logic is flawed but your result is correct. The "L" prefix is a sort of precompiler literal Microsoft has put in place it's not a macro or such and it is sort of bound like the quotes are in the precompiler look at how you write a normal string constants const char* txtcnst = "SOME_CONSTANT_TEXT"; There is no "&" pointer there so why just because you want a unicode string would they change it The answer is it is the way it is because that is how C language defined the use of constants and think of L as part of the quotes "" simply telling the compiler the text is unicode format.

          In vino veritas

          CPalliniC 1 Reply Last reply
          0
          • L leon de boer

            You logic is flawed but your result is correct. The "L" prefix is a sort of precompiler literal Microsoft has put in place it's not a macro or such and it is sort of bound like the quotes are in the precompiler look at how you write a normal string constants const char* txtcnst = "SOME_CONSTANT_TEXT"; There is no "&" pointer there so why just because you want a unicode string would they change it The answer is it is the way it is because that is how C language defined the use of constants and think of L as part of the quotes "" simply telling the compiler the text is unicode format.

            In vino veritas

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            As far as I know L is not a Microsoft thing, see, for instance "string literal" at cppreference.com[^].

            Veni, vidi, vici.

            In testa che avete, signor di Ceprano?

            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