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 const

Pointer to const

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 4 Posters 1 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.
  • L Offline
    L Offline
    LiYS
    wrote on last edited by
    #1

    void u(const int* cip)
    {
    //! *cip = 2; // Illegal -- modifies value
    int i = *cip; // OK -- copies value
    //! int* ip2 = cip; // Illegal: non-const
    }

    In the snippet above, the argument "const int* cip" is a pointer to const that is to say the thing this pointer pointed to can't be changed but the pointer can be changed right? But I don't understand why the line "int* ip2 = cip;" can't passed the compilation. Is it because after its execution the pointee can be changed through ip2?


    ...always look on the bright side of life... (Whistle)

    B N M 3 Replies Last reply
    0
    • L LiYS

      void u(const int* cip)
      {
      //! *cip = 2; // Illegal -- modifies value
      int i = *cip; // OK -- copies value
      //! int* ip2 = cip; // Illegal: non-const
      }

      In the snippet above, the argument "const int* cip" is a pointer to const that is to say the thing this pointer pointed to can't be changed but the pointer can be changed right? But I don't understand why the line "int* ip2 = cip;" can't passed the compilation. Is it because after its execution the pointee can be changed through ip2?


      ...always look on the bright side of life... (Whistle)

      B Offline
      B Offline
      Bob Stanneveld
      wrote on last edited by
      #2

      The reason why the compiler complains is that you want to do an assignment where the original value loses qualifiers. If I remember correctly, you can do the following:

      void u(const int* cip)
      {
      int** ppi = &cip;
      (*(*ppi)) = 6;
      // cip points now to 6
      (*(*ppi)) = 8;
      // cip points now to 8!
      }

      This is one of the many reasons why pointer magic should be avoided as much as possible. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

      L 1 Reply Last reply
      0
      • L LiYS

        void u(const int* cip)
        {
        //! *cip = 2; // Illegal -- modifies value
        int i = *cip; // OK -- copies value
        //! int* ip2 = cip; // Illegal: non-const
        }

        In the snippet above, the argument "const int* cip" is a pointer to const that is to say the thing this pointer pointed to can't be changed but the pointer can be changed right? But I don't understand why the line "int* ip2 = cip;" can't passed the compilation. Is it because after its execution the pointee can be changed through ip2?


        ...always look on the bright side of life... (Whistle)

        N Offline
        N Offline
        Nish Nishant
        wrote on last edited by
        #3

        Try :- int* ip2 = const_cast<int*>(cip);

        1 Reply Last reply
        0
        • L LiYS

          void u(const int* cip)
          {
          //! *cip = 2; // Illegal -- modifies value
          int i = *cip; // OK -- copies value
          //! int* ip2 = cip; // Illegal: non-const
          }

          In the snippet above, the argument "const int* cip" is a pointer to const that is to say the thing this pointer pointed to can't be changed but the pointer can be changed right? But I don't understand why the line "int* ip2 = cip;" can't passed the compilation. Is it because after its execution the pointee can be changed through ip2?


          ...always look on the bright side of life... (Whistle)

          M Offline
          M Offline
          MaxVampire82
          wrote on last edited by
          #4

          ip2 isn't a pointer to const.

          1 Reply Last reply
          0
          • B Bob Stanneveld

            The reason why the compiler complains is that you want to do an assignment where the original value loses qualifiers. If I remember correctly, you can do the following:

            void u(const int* cip)
            {
            int** ppi = &cip;
            (*(*ppi)) = 6;
            // cip points now to 6
            (*(*ppi)) = 8;
            // cip points now to 8!
            }

            This is one of the many reasons why pointer magic should be avoided as much as possible. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

            L Offline
            L Offline
            LiYS
            wrote on last edited by
            #5

            Doing "int* ip2 = cip" illegal only because the pointee of pointer cip could be changed through pointer ip2? In that case the constness of pointer to const can't be keep?


            ...always look on the bright side of life... (Whistle)

            B 1 Reply Last reply
            0
            • L LiYS

              Doing "int* ip2 = cip" illegal only because the pointee of pointer cip could be changed through pointer ip2? In that case the constness of pointer to const can't be keep?


              ...always look on the bright side of life... (Whistle)

              B Offline
              B Offline
              Bob Stanneveld
              wrote on last edited by
              #6

              YongSheng Li wrote: Doing "int* ip2 = cip" illegal only because the pointee of pointer cip could be changed through pointer ip2? In that case the constness of pointer to const can't be keep? Thats exactly what I'm saying. Doing that, the original type loses qualifiers, which is illegal.. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

              L 1 Reply Last reply
              0
              • B Bob Stanneveld

                YongSheng Li wrote: Doing "int* ip2 = cip" illegal only because the pointee of pointer cip could be changed through pointer ip2? In that case the constness of pointer to const can't be keep? Thats exactly what I'm saying. Doing that, the original type loses qualifiers, which is illegal.. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

                L Offline
                L Offline
                LiYS
                wrote on last edited by
                #7

                That's exactly what I'm expecting!Thank you!:-D


                ...always look on the bright side of life... (Whistle)

                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