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. C Basic

C Basic

Scheduled Pinned Locked Moved C / C++ / MFC
10 Posts 8 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.
  • J Offline
    J Offline
    john5632
    wrote on last edited by
    #1

    I am not able to understand the output of below lines. As per my understanding output should be 8 because we are subtracting int address which is having 8 byte diffrence but output is coming 2. Please expalin.

    int a[]={5,10,15,20};
    int d = &a[2]-&a[0];

    S _ D L J 6 Replies Last reply
    0
    • J john5632

      I am not able to understand the output of below lines. As per my understanding output should be 8 because we are subtracting int address which is having 8 byte diffrence but output is coming 2. Please expalin.

      int a[]={5,10,15,20};
      int d = &a[2]-&a[0];

      S Offline
      S Offline
      SandipG
      wrote on last edited by
      #2

      Read about Pointer arithmetic[^] I hope it helps.

      1 Reply Last reply
      0
      • J john5632

        I am not able to understand the output of below lines. As per my understanding output should be 8 because we are subtracting int address which is having 8 byte diffrence but output is coming 2. Please expalin.

        int a[]={5,10,15,20};
        int d = &a[2]-&a[0];

        _ Offline
        _ Offline
        _AnsHUMAN_
        wrote on last edited by
        #3

        See this;

        &a[2] - &a[0]
        (a + 2) - (a + 0)
        a + 2 - a - 0
        2 - 0
        2

        the above is true because use pointers you will always get the difference of elements

        You talk about Being HUMAN. I have it in my name AnsHUMAN

        D 1 Reply Last reply
        0
        • _ _AnsHUMAN_

          See this;

          &a[2] - &a[0]
          (a + 2) - (a + 0)
          a + 2 - a - 0
          2 - 0
          2

          the above is true because use pointers you will always get the difference of elements

          You talk about Being HUMAN. I have it in my name AnsHUMAN

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          _AnsHUMAN_ wrote:

          a + 2 - a - 0 2 - 0

          While your end result is correct, shouldn't that be: a + 2 - a + 0 2 + 0

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

          1 Reply Last reply
          0
          • J john5632

            I am not able to understand the output of below lines. As per my understanding output should be 8 because we are subtracting int address which is having 8 byte diffrence but output is coming 2. Please expalin.

            int a[]={5,10,15,20};
            int d = &a[2]-&a[0];

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            If the address of (the first item in) a is, for example, 0x1000 and each item in a is 4 bytes, then the difference between the third item (0x1008) and the first item would be 8 divided by the size of an int (most likely 4), thus 2 would be the output.

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

            1 Reply Last reply
            0
            • J john5632

              I am not able to understand the output of below lines. As per my understanding output should be 8 because we are subtracting int address which is having 8 byte diffrence but output is coming 2. Please expalin.

              int a[]={5,10,15,20};
              int d = &a[2]-&a[0];

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

              You are correct, the differene in the addresses is 8. I can only assume the compiler is dividing that by the type of dm which being an int, makes the result 2. Did you try it with char d = &a[2] - &a[0] ? It wold be interesting. IMO the compiler shouldnt do this, it is unexpected. I would expect to get 8 back, after all, that is the difference in the pointers.

              ============================== Nothing to say.

              C L 2 Replies Last reply
              0
              • J john5632

                I am not able to understand the output of below lines. As per my understanding output should be 8 because we are subtracting int address which is having 8 byte diffrence but output is coming 2. Please expalin.

                int a[]={5,10,15,20};
                int d = &a[2]-&a[0];

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

                What happens if you do the following?

                int d = ((void*)&a[2])-((void*)&a[0]);

                1 Reply Last reply
                0
                • L Lost User

                  You are correct, the differene in the addresses is 8. I can only assume the compiler is dividing that by the type of dm which being an int, makes the result 2. Did you try it with char d = &a[2] - &a[0] ? It wold be interesting. IMO the compiler shouldnt do this, it is unexpected. I would expect to get 8 back, after all, that is the difference in the pointers.

                  ============================== Nothing to say.

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

                  Erudite_Eric wrote:

                  IMO the compiler shouldnt do this, it is unexpected.

                  Quite the opposite, the compilers must do that, because it is a C programming language rule (you may find it, for instance, in the B&K book).

                  Veni, vidi, vici.

                  1 Reply Last reply
                  0
                  • L Lost User

                    You are correct, the differene in the addresses is 8. I can only assume the compiler is dividing that by the type of dm which being an int, makes the result 2. Did you try it with char d = &a[2] - &a[0] ? It wold be interesting. IMO the compiler shouldnt do this, it is unexpected. I would expect to get 8 back, after all, that is the difference in the pointers.

                    ============================== Nothing to say.

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

                    Erudite_Eric wrote:

                    You are correct, the differene in the addresses is 8.

                    On the contrary he is totally wrong. The difference is the count of elements in the array, between the first and last item. This is C language, not machine code, as designed by Kernighan & Ritchie.

                    Use the best guess

                    1 Reply Last reply
                    0
                    • J john5632

                      I am not able to understand the output of below lines. As per my understanding output should be 8 because we are subtracting int address which is having 8 byte diffrence but output is coming 2. Please expalin.

                      int a[]={5,10,15,20};
                      int d = &a[2]-&a[0];

                      H Offline
                      H Offline
                      hans sch
                      wrote on last edited by
                      #10

                      You can subtract two pointers of the same base type, and the result is the number of elements of the base type that fit between the addresses which the pointers hold. Likewise, you can add an integer value to a pointer, and that will result in the address of a data type instance so and so many steps above the original pointer. Confusing? - Take your example:

                      int a[]={5,10,15,20};

                      Assume a starts at address 0x00040000. This is also the address of a[0]. In C, a is the same as &a[0]. a+1 is &a[1] or 0x00040004, and a+2 is &a[2] or 0x00040008. &a[2]-2 is &a[0] or 0x00040000. When x-y==z is true, then x-z==y should also be true; when &a[2]-2==&a[0] is true, then &a[2]-&a[0]==2 should also be true. The C language is nice enough to fulfil this. To calculate the number of bytes between two memory locations, convert the pointers to BYTE* like this:

                      (BYTE*)&a[2]-(BYTE*)&a[0]

                      This would evaluate to 8, as you expected. Assuming, of course, that 'int' is a 32-bit integer type.

                      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