How does the compiler calculates the address of the last element in the below array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • radha gogia
    New Member
    • Feb 2015
    • 56

    How does the compiler calculates the address of the last element in the below array?

    Say I have an array of a[5] , and base address is 3000 now , and I have to get the address of the last element a[4] and I have a pointer b , and I do
    b=a+n-1 , so I have one confusion in this that first a+n would be executed like
    a+n*sizeof(a) -1 ,now a+n*sizeof(a)= 3010 ,now when we do -1 from it , how does it evaluate to 3008 , the address of last element since contiguous memory cells are from address 3000 to 3008 so then how come 3010-1 evaluates to 3008 ,how does compiler come to know how much bytes it has to move backwards since 3010 is a random location .
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    All you need do is:

    Code:
    b = a+4;
    In doing pointer arithmetic, the compiler will add 4 times the sizeof the array type. You don't have to worry about that.

    In your example, if n is number of elements in the array, and b is a pointer to the array type, then you would:

    Code:
    b =  a + n - 1;
    You might also read: http://bytes.com/topic/c/insights/77...rrays-revealed

    Comment

    • radha gogia
      New Member
      • Feb 2015
      • 56

      #3
      Sorry but couldn't get clear with this concept that how 3010 -1 =3008 when that is some random location in the memory as array is contiguously located from 3000 to 3008 only .
      As u said that compiler will add 4 times the sizeof the array type but no of elements in the array are 5 and not 4 so then ?

      Sorry but seriously I am stucked at this point .

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        The first element of an array is element 0. The name of the array is the address of that element. Therefore, that address plus 4 is the address of the fifth element,which is what you asked about.

        Did you read the article?

        Comment

        • radha gogia
          New Member
          • Feb 2015
          • 56

          #5
          Yes , I read and agree to your explanation but what I am saying ,plz see it once, I am confused in that point.

          Comment

          • radha gogia
            New Member
            • Feb 2015
            • 56

            #6
            Since b=a+n-1 =3010-1=3008 , how's that evaluating to 3008 when 3010 is a random location.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              Let's be very specific. Let's assume for a moment that data pointers can be cast to integer type unsigned long.
              Code:
              #define N 5
              int main()
              {
                int a[N];
                int *b;
                unsigned long p;
              
                b = a;
                p = (unsigned long)a;
                printf("1a. %p\n", b);
                printf("1b. %ld\n", p);
              
                b = a + N;
                p = (unsigned long)a + N*sizeof(int);
                printf("2a. %p\n", b);
                printf("2b. %ld\n", p);
              
                b = a + N - 1;
                p = (unsigned long)a + (N-1)*sizeof(int);
                printf("3a. %p\n", b);
                printf("2b. %ld\n", p);
              }
              If sizeof(int) is 4, and if array a happens to start at address 3000, then I expect the output to be:
              Code:
              1a. 3000
              1b. 3000
              2a. 3020
              2b. 3020
              3a. 3016
              3b. 3016
              Pointer arithmetic involves an arbitrary but well-defined pointer value and an arbitrary but well-defined integer expression. Nothing is random.

              Pointer arithmetic works as if it were implemented in accordance with the p expressions above.

              Comment

              Working...