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

Pointer indirection

Scheduled Pinned Locked Moved C / C++ / MFC
question
12 Posts 5 Posters 49 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.
  • C Calin Negru

    Hi, Is this how it should be done? int Strawberry = 10; int * PointerInd0 = &Strawberry; int ** PointerInd1 = &PointerInd0; // I’m not sure what comes next int *** PointerInd2 = &PointerInd1; Sorry I’m not enclosing the source code in code tags, I don’t have edit options on mobile.

    Mircea NeacsuM Offline
    Mircea NeacsuM Offline
    Mircea Neacsu
    wrote on last edited by
    #2

    Calin Negru wrote:

    // I’m not sure what comes next
    int *** PointerLvl3 = &PointerLvl2;

    Yes, indeed. You can continue with as many levels of indirection as you wish (although I don't remember needing more than two).

    Mircea

    C 1 Reply Last reply
    0
    • Mircea NeacsuM Mircea Neacsu

      Calin Negru wrote:

      // I’m not sure what comes next
      int *** PointerLvl3 = &PointerLvl2;

      Yes, indeed. You can continue with as many levels of indirection as you wish (although I don't remember needing more than two).

      Mircea

      C Offline
      C Offline
      Calin Negru
      wrote on last edited by
      #3

      If that is true then this simple assign address to is a bit deceiving because you still have to remember what PointerInd2 is, you need special syntax to access Strawberry from PointerInd2 ***PointerInd2 = 31; // just a guess

      Mircea NeacsuM K 2 Replies Last reply
      0
      • C Calin Negru

        If that is true then this simple assign address to is a bit deceiving because you still have to remember what PointerInd2 is, you need special syntax to access Strawberry from PointerInd2 ***PointerInd2 = 31; // just a guess

        Mircea NeacsuM Offline
        Mircea NeacsuM Offline
        Mircea Neacsu
        wrote on last edited by
        #4

        Calin Negru wrote:

        ***PointerInd2 = 31; // just a guess

        Yes, nothing wrong with that. I don't see why you see it as deceiving. If you miss one of the indirection operators and you write:

        **PointerInd2 = 31;

        compiler will flag it with an error like "cannot assign 'int' to 'int*' ". But, again, cases where you need more than two level on indirection are exceedingly rare. Probably because we humans aren't great at keeping track of stack levels.

        Mircea

        C 2 Replies Last reply
        0
        • C Calin Negru

          If that is true then this simple assign address to is a bit deceiving because you still have to remember what PointerInd2 is, you need special syntax to access Strawberry from PointerInd2 ***PointerInd2 = 31; // just a guess

          K Offline
          K Offline
          k5054
          wrote on last edited by
          #5

          Correct, I think. A simple test program would confirm that, but I'm not feeling the urge. But if you need more than two or three levels of indirection, then the problem space should lead to sensible variable naming. That and intelligent comments should make your intent clear. And, of course, you've got typedefs or C++ using statements to help reduce the brain cramp that I find multiple indirection sometimes brings. If you're using C++, you also have references which might help reduce the (apparent) levels of indirection going on.

          Keep Calm and Carry On

          1 Reply Last reply
          0
          • C Calin Negru

            Hi, Is this how it should be done? int Strawberry = 10; int * PointerInd0 = &Strawberry; int ** PointerInd1 = &PointerInd0; // I’m not sure what comes next int *** PointerInd2 = &PointerInd1; Sorry I’m not enclosing the source code in code tags, I don’t have edit options on mobile.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6
            int Strawberry = 10;
            int \* PointerInd0 = &Strawberry;
            std::cout << "\*PointerInd0: " << \*PointerInd0 << std::endl;
            int \*\* PointerInd1 = &PointerInd0;
            std::cout << "\*\*PointerInd1: " << \*\*PointerInd1 << std::endl;
            // I’m not sure what comes next
            int \*\*\* PointerInd2 = &PointerInd1;
            std::cout << "\*\*\*PointerInd2: " << \*\*\*PointerInd2 << std::endl;
            int \*\*\*\* PointerInd3 = &PointerInd2;
            std::cout << "\*\*\*\*PointerInd3: " << \*\*\*\*PointerInd3 << std::endl;
            

            Results:

            *PointerInd0: 10
            **PointerInd1: 10
            ***PointerInd2: 10
            ****PointerInd3: 10

            And so until the compiler or the application gives up. If you are really interested then get an assembly listing and see what the machine code is doing.

            C 1 Reply Last reply
            0
            • Mircea NeacsuM Mircea Neacsu

              Calin Negru wrote:

              ***PointerInd2 = 31; // just a guess

              Yes, nothing wrong with that. I don't see why you see it as deceiving. If you miss one of the indirection operators and you write:

              **PointerInd2 = 31;

              compiler will flag it with an error like "cannot assign 'int' to 'int*' ". But, again, cases where you need more than two level on indirection are exceedingly rare. Probably because we humans aren't great at keeping track of stack levels.

              Mircea

              C Offline
              C Offline
              Calin Negru
              wrote on last edited by
              #7

              >I don’t see why you see it as deceiving It’s just a single &sign someone could expect it to behave like a standard pointer , instead you have to track down the indirection pointer to where it was declared and observe all the detail (the extra stars)

              1 Reply Last reply
              0
              • L Lost User
                int Strawberry = 10;
                int \* PointerInd0 = &Strawberry;
                std::cout << "\*PointerInd0: " << \*PointerInd0 << std::endl;
                int \*\* PointerInd1 = &PointerInd0;
                std::cout << "\*\*PointerInd1: " << \*\*PointerInd1 << std::endl;
                // I’m not sure what comes next
                int \*\*\* PointerInd2 = &PointerInd1;
                std::cout << "\*\*\*PointerInd2: " << \*\*\*PointerInd2 << std::endl;
                int \*\*\*\* PointerInd3 = &PointerInd2;
                std::cout << "\*\*\*\*PointerInd3: " << \*\*\*\*PointerInd3 << std::endl;
                

                Results:

                *PointerInd0: 10
                **PointerInd1: 10
                ***PointerInd2: 10
                ****PointerInd3: 10

                And so until the compiler or the application gives up. If you are really interested then get an assembly listing and see what the machine code is doing.

                C Offline
                C Offline
                Calin Negru
                wrote on last edited by
                #8

                Thank you Richard, you make it clear.

                L 1 Reply Last reply
                0
                • Mircea NeacsuM Mircea Neacsu

                  Calin Negru wrote:

                  ***PointerInd2 = 31; // just a guess

                  Yes, nothing wrong with that. I don't see why you see it as deceiving. If you miss one of the indirection operators and you write:

                  **PointerInd2 = 31;

                  compiler will flag it with an error like "cannot assign 'int' to 'int*' ". But, again, cases where you need more than two level on indirection are exceedingly rare. Probably because we humans aren't great at keeping track of stack levels.

                  Mircea

                  C Offline
                  C Offline
                  Calin Negru
                  wrote on last edited by
                  #9

                  >I don’t see why you see it as deceiving It’s just a single &sign one that could make you believe it’s a typical pointer you’re dealing with. You need to be in a high alert so to speak and always have in mind what you have declared.

                  J 1 Reply Last reply
                  0
                  • C Calin Negru

                    >I don’t see why you see it as deceiving It’s just a single &sign one that could make you believe it’s a typical pointer you’re dealing with. You need to be in a high alert so to speak and always have in mind what you have declared.

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #10

                    Calin Negru wrote:

                    you believe it’s a typical pointer you’re dealing with.

                    The developer should of course be aware of the code they are writing/modifying. And this should not be something that is used very often.

                    C 1 Reply Last reply
                    0
                    • J jschell

                      Calin Negru wrote:

                      you believe it’s a typical pointer you’re dealing with.

                      The developer should of course be aware of the code they are writing/modifying. And this should not be something that is used very often.

                      C Offline
                      C Offline
                      Calin Negru
                      wrote on last edited by
                      #11

                      >The developer should of course be aware I know. If you look at the address assignation alone you don’t have a clue what that is (Something = &SomethingElse) you have look at the declaration to figure it out. Sorry if that sounds like repeating the same thing. What I’m saying is that it’s not your C# fire and forget.

                      1 Reply Last reply
                      0
                      • C Calin Negru

                        Thank you Richard, you make it clear.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #12

                        You're welcome. It always helps to look at actual code.

                        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