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. ++ operator in the case of pointers

++ operator in the case of pointers

Scheduled Pinned Locked Moved C / C++ / MFC
question
11 Posts 5 Posters 3 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 Offline
    C Offline
    Calin Negru
    wrote on last edited by
    #1

    I have some basic knowledge about pointers and I`m trying to get an exhaustive understanding, like an all sides perspective. My question is what does the operator ++ do to a pointer? I`ve seen it being used in linked lists related code.

    L K 2 Replies Last reply
    0
    • C Calin Negru

      I have some basic knowledge about pointers and I`m trying to get an exhaustive understanding, like an all sides perspective. My question is what does the operator ++ do to a pointer? I`ve seen it being used in linked lists related code.

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

      It is the increment operator, and as with any variable it adds 1 to the pointer. This will move the pointer forward by one element of whatever it is pointing to: int, long, struct, object etc.

      C 1 Reply Last reply
      0
      • L Lost User

        It is the increment operator, and as with any variable it adds 1 to the pointer. This will move the pointer forward by one element of whatever it is pointing to: int, long, struct, object etc.

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

        could I increment not the value of the variable the pointer is pointing to but the pointer itself?

        int Something0 = 0;
        int Something1 = 1;
        int ** Pointers = new int*[2];
        Pointers[0] = &Something0;
        *Pointers[0]++; //increase Something0 by 1
        Pointers[1] = &Something1;
        *Pointers[1]++; //increase Something1 by 1

        //Could I do this
        **Pointers =10; //set Something0 to 10
        Pointers++;
        **Pointers =30; //set Something1 to 30

        Richard Andrew x64R 1 Reply Last reply
        0
        • C Calin Negru

          could I increment not the value of the variable the pointer is pointing to but the pointer itself?

          int Something0 = 0;
          int Something1 = 1;
          int ** Pointers = new int*[2];
          Pointers[0] = &Something0;
          *Pointers[0]++; //increase Something0 by 1
          Pointers[1] = &Something1;
          *Pointers[1]++; //increase Something1 by 1

          //Could I do this
          **Pointers =10; //set Something0 to 10
          Pointers++;
          **Pointers =30; //set Something1 to 30

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          That's what the increment operator does to a pointer - it increments the pointer itself. If you want to increment the value that it's pointing to, you must dereference the pointer first, like so: (*pointer)++;

          The difficult we do right away... ...the impossible takes slightly longer.

          C 1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            That's what the increment operator does to a pointer - it increments the pointer itself. If you want to increment the value that it's pointing to, you must dereference the pointer first, like so: (*pointer)++;

            The difficult we do right away... ...the impossible takes slightly longer.

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

            Thanks I think I understand.

            1 Reply Last reply
            0
            • C Calin Negru

              I have some basic knowledge about pointers and I`m trying to get an exhaustive understanding, like an all sides perspective. My question is what does the operator ++ do to a pointer? I`ve seen it being used in linked lists related code.

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

              You should also know about the difference between a pre-increment and post-increment operator. In an expression, a pre-increment operator computes the increment before accessing the item, and a post-increment computes the increment afterwards. For example

              int i = 1;
              int j = ++i; // i is incremented before assignment: i = 2, j = 2;
              int k = i++; // i is incremented after assignment: i = 3, k = 2;
              int x = ++i++; // UNDEFINED BEHAVIOR!!! Do not do this!!!

              In the case ++i++ The standard does not say what will happen. The only thing you know is that i will have the value 5 after the expression is complete. The variable x could be assigned at any point during the expression, or since this is undefined behavior, it might even get the value -42, which makes no sense in respect to the code, but is acceptable according to C/C++ standard. Even if you do some testing and work out that for type int the expression ++i++ is the equivalent of ++(i++), it might be different for type long, or for a pointer type, and it might change depending on complexity of the overall expression, and/or the optimization level. The takeaway for this is that you don't know what will happen if you try to pre and post increment an expressions term at the same time. Moreover, you should not try to increment the same variable within the same expression:

              int i = 2;
              printf("%d %d\n", i++, ++); // undefined order of evaluation

              In the printf statement above, its undefined which order the arguments to printf get evaluated, and different compilers do it differently. I'm reasonably sure that clang and gcc do this differently.

              Keep Calm and Carry On

              C J 2 Replies Last reply
              0
              • K k5054

                You should also know about the difference between a pre-increment and post-increment operator. In an expression, a pre-increment operator computes the increment before accessing the item, and a post-increment computes the increment afterwards. For example

                int i = 1;
                int j = ++i; // i is incremented before assignment: i = 2, j = 2;
                int k = i++; // i is incremented after assignment: i = 3, k = 2;
                int x = ++i++; // UNDEFINED BEHAVIOR!!! Do not do this!!!

                In the case ++i++ The standard does not say what will happen. The only thing you know is that i will have the value 5 after the expression is complete. The variable x could be assigned at any point during the expression, or since this is undefined behavior, it might even get the value -42, which makes no sense in respect to the code, but is acceptable according to C/C++ standard. Even if you do some testing and work out that for type int the expression ++i++ is the equivalent of ++(i++), it might be different for type long, or for a pointer type, and it might change depending on complexity of the overall expression, and/or the optimization level. The takeaway for this is that you don't know what will happen if you try to pre and post increment an expressions term at the same time. Moreover, you should not try to increment the same variable within the same expression:

                int i = 2;
                printf("%d %d\n", i++, ++); // undefined order of evaluation

                In the printf statement above, its undefined which order the arguments to printf get evaluated, and different compilers do it differently. I'm reasonably sure that clang and gcc do this differently.

                Keep Calm and Carry On

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

                thanks k5054. I`m not sure I fully understand the difference. I guess I need look up the issue in several different sources. (I often find consulting different perspectives on the same issue helpful).

                L 1 Reply Last reply
                0
                • C Calin Negru

                  thanks k5054. I`m not sure I fully understand the difference. I guess I need look up the issue in several different sources. (I often find consulting different perspectives on the same issue helpful).

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

                  You should also read Why Does x = ++x + x++ Give Me the Wrong Answer?[^].

                  C 1 Reply Last reply
                  0
                  • K k5054

                    You should also know about the difference between a pre-increment and post-increment operator. In an expression, a pre-increment operator computes the increment before accessing the item, and a post-increment computes the increment afterwards. For example

                    int i = 1;
                    int j = ++i; // i is incremented before assignment: i = 2, j = 2;
                    int k = i++; // i is incremented after assignment: i = 3, k = 2;
                    int x = ++i++; // UNDEFINED BEHAVIOR!!! Do not do this!!!

                    In the case ++i++ The standard does not say what will happen. The only thing you know is that i will have the value 5 after the expression is complete. The variable x could be assigned at any point during the expression, or since this is undefined behavior, it might even get the value -42, which makes no sense in respect to the code, but is acceptable according to C/C++ standard. Even if you do some testing and work out that for type int the expression ++i++ is the equivalent of ++(i++), it might be different for type long, or for a pointer type, and it might change depending on complexity of the overall expression, and/or the optimization level. The takeaway for this is that you don't know what will happen if you try to pre and post increment an expressions term at the same time. Moreover, you should not try to increment the same variable within the same expression:

                    int i = 2;
                    printf("%d %d\n", i++, ++); // undefined order of evaluation

                    In the printf statement above, its undefined which order the arguments to printf get evaluated, and different compilers do it differently. I'm reasonably sure that clang and gcc do this differently.

                    Keep Calm and Carry On

                    J Offline
                    J Offline
                    jsc42
                    wrote on last edited by
                    #9

                    I am guessing that

                    k5054 wrote:

                    int i = 2;
                    printf("%d %d\n", i++, ++); // undefined order of evaluation

                    should have read as

                    int i = 2;
                    printf("%d %d\n", i++, ++i); // undefined order of evaluation

                    K 1 Reply Last reply
                    0
                    • J jsc42

                      I am guessing that

                      k5054 wrote:

                      int i = 2;
                      printf("%d %d\n", i++, ++); // undefined order of evaluation

                      should have read as

                      int i = 2;
                      printf("%d %d\n", i++, ++i); // undefined order of evaluation

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

                      Yes, of course. My bad.

                      Keep Calm and Carry On

                      1 Reply Last reply
                      0
                      • L Lost User

                        You should also read Why Does x = ++x + x++ Give Me the Wrong Answer?[^].

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

                        Thanks

                        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