array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • destination
    New Member
    • Dec 2009
    • 34

    array

    int a[10];
    why does
    printf("%p",a); and printf("%p",&a) ;
    print the same result.

    also printf("%p",&(a )); //this works fine
    but printf("%p",.&( a+2));//results into error.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Because a and &a point at the same thing (an array) the difference is in the type.

    a has the type int * the same as &a[0]

    &a has the type int (*)[10]

    But the location of both the array and the first integer in the array are the same so a and &a have the same value for any platform where the pointer to integer and pointer to structure are semantically the same.

    &(a+2) doesn't work because the address (a+2) is not an object (not an l-value) so you can't get an address of it.

    Comment

    • destination
      New Member
      • Dec 2009
      • 34

      #3
      Banfa
      You mean to say address of a[0] and a are stored at the same location.
      Also what should &a+2 yield??

      Comment

      • puneetsardana88
        New Member
        • Aug 2009
        • 57

        #4
        a is a pointer and a[0] is *(a+0). I think you mean to say *a and a[0] are stored in same location. I think yes.

        &a+2 Since type of &a is (int*)[10]

        Base address of a + size of array*size of pointer to integer.


        Please correct me if i am wrong

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          You mean to say address of a[0] and a are stored at the same location.
          Also what should &a+2 yield??
          If you meant a[0] and a are stored at the same location then yes.
          If you meant address of a[0] and address of a are the same then yes.
          But if you meant (address of a[0]) and a are stored at the same location then no. (address of a[0]) has no location in memory since it is not an object.

          &a+2

          &a has type int(*)[10], a pointer to an array of 10 integers.

          Assuming that integer has size 4 (which is very common now) then this is basic pointer arithmetic, the result is the original pointer plus 2 times the size of the object pointed to. Since the object pointed to is an array of 10 integers its size is 10 * sizeof(int) or 40 under our assumptions. so the expression

          &a+2

          is equivalent to

          (char*)(&a) + (40 * 2)

          in words

          Base address of a + (size of array * increment)

          The result is the address 80 bytes further along in memory.

          Comment

          • destination
            New Member
            • Dec 2009
            • 34

            #6
            Banfa
            i read ur above post and it cleared my doubt but still there are some doubts somewhere in my mind.pls answer it.

            Let us say i have int a[3]={1}
            .Its first element i.e. a[0] will be stored at some location say 23FF60.If i write &a[0] i will get this value(23FF60) again if i write a i will get the same value.
            My question is why are address of a[0] (i.e. &a[0]) and address of a(&a) are stored at the same location?

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              The address of a[0] and a are not stored anywhere, they are a property of a[0] and a. The address of a[0] and the address of a are the same because a[0] is the first element in a.

              Think of a row of cars parked nose to tail. The start of the row of cars and the start of the first car in the row are at the same place. That place is not stored anywhere because there is no place for storing places on the street, there is only places for storing cars.

              Comment

              • destination
                New Member
                • Dec 2009
                • 34

                #8
                Car parking example was awesome.Really a gr8 explanation
                A big thanx to you Banfa.You are gr8.

                Comment

                Working...