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. The difference between...

The difference between...

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

    Hi all, This may seem very elementary, but why "&a + 1" and "a + 1" are different in the following code

    int a\[5\] = {1, 2, 3, 4, 5};
    int\* ptr = (int\*)(&a + 1);
    printf("%d, %d", \*(a + 1), \*(ptr-1));
    

    Thanks,


    P C 2 Replies Last reply
    0
    • L LiYS

      Hi all, This may seem very elementary, but why "&a + 1" and "a + 1" are different in the following code

      int a\[5\] = {1, 2, 3, 4, 5};
      int\* ptr = (int\*)(&a + 1);
      printf("%d, %d", \*(a + 1), \*(ptr-1));
      

      Thanks,


      P Offline
      P Offline
      Perspx
      wrote on last edited by
      #2

      *(a + 1) returns a pointer to the 2nd element in a, which here is 2; a + 2 would return the 3rd element in a, which is 3, and so on. (&a + 1) returns &a, which is a pointer of a, and as pointer arithmetic is used here, +1 increments this address by 20, as a has a size of 20 (5 ints or 5 * 4 bytes), which would point to the next memory block AFTER a. When you print (ptr-1) this decreases the pointer address by 4 (4 bytes to an int), which returns the 5th element in the array, which is 5. Hope this helps, --Perspx

      "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript

      L 1 Reply Last reply
      0
      • L LiYS

        Hi all, This may seem very elementary, but why "&a + 1" and "a + 1" are different in the following code

        int a\[5\] = {1, 2, 3, 4, 5};
        int\* ptr = (int\*)(&a + 1);
        printf("%d, %d", \*(a + 1), \*(ptr-1));
        

        Thanks,


        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        So, what was the question? BTW try also

        printf("%d, %d\n", sizeof(a), sizeof(int*));

        :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [Image resize DLL]

        1 Reply Last reply
        0
        • P Perspx

          *(a + 1) returns a pointer to the 2nd element in a, which here is 2; a + 2 would return the 3rd element in a, which is 3, and so on. (&a + 1) returns &a, which is a pointer of a, and as pointer arithmetic is used here, +1 increments this address by 20, as a has a size of 20 (5 ints or 5 * 4 bytes), which would point to the next memory block AFTER a. When you print (ptr-1) this decreases the pointer address by 4 (4 bytes to an int), which returns the 5th element in the array, which is 5. Hope this helps, --Perspx

          "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript

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

          I still don't understand. To me "a" is of type a[5] and "&a" is of type pointer, so they're supposed to calculated in 20 and 4 bytes unit respectively, but that's contrary to the actual result.


          O T 2 Replies Last reply
          0
          • L LiYS

            I still don't understand. To me "a" is of type a[5] and "&a" is of type pointer, so they're supposed to calculated in 20 and 4 bytes unit respectively, but that's contrary to the actual result.


            O Offline
            O Offline
            oobimoo
            wrote on last edited by
            #5

            I understand your doubts. Arrays on stack and dynamically allocated arrays have differences, but we must have a uniform way to access elements with pointers. From this point of view 'a' must be an int*. On the other hand 'a' must be like any other stack object (struct or class). We must be able to take the actuall size with the sizeof operator or access the elements by casting the base address '&a'. For reasons like the above we see 'strange' things like &a == a

            1 Reply Last reply
            0
            • L LiYS

              I still don't understand. To me "a" is of type a[5] and "&a" is of type pointer, so they're supposed to calculated in 20 and 4 bytes unit respectively, but that's contrary to the actual result.


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

              a is an array of 5 ints. so a is the address of the 1st element of the array, and is of type int* (if we consider an int to be 4 bytes, incrementing a will move the pointer 4 bytes further). &a however is the address of the variable a itself, and is of type int[5] (if we consider an int to be 4 bytes, int[5] weights 20 bytes, so incrementing &a will move the pointer 20 bytes further). as suggested by CPallini, have you tried to print some sizeof to check that ?

              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

              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