Re: I confused Address of struct member like this, please explain,thanks

Discussion in 'C++' started by Victor Bazarov, May 28, 2013.

  1. On 5/28/2013 5:49 AM, Ed wrote:
    > typedef struct Test_tag
    > {
    > int a;
    > char Data[32];
    >
    > } Test;
    >
    >
    > int main(int argc, char *argv[])
    > {
    >
    > Test o;
    > char arr[64];
    > Test *pObj = (Test*) arr;
    > int* p = (int*)arr;
    > printf("p:%x, &p:%x\n", p, &p);
    > printf("pObj->Data:%x, &pOjb->Data:%x\n", pObj->Data, &pObj->Data);
    > printf("o.Data:%x, &o.Data:%x\n", o.Data, &o.Data);
    > printf("o.a:%x, &o.a:%x\n", o.a, &o.a);
    >
    > return 0;
    > }
    > ===========================================
    > result:
    > (1) p:22ff00, &p:22fef8
    > (2) pObj->Data:22ff04, &pOjb->Data:22ff04
    > (3) o.Data:22ff44, &o.Data:22ff44
    > (4) o.a:4013b0, &o.a:22ff40
    >
    > my question:
    > why these address is same in (2) and (3)?


    It has nothing to do with the fact that 'Data' is a member. It has
    everything to do with the fact that the address of the first element of
    an array and the address of the array are one and the same, when you
    take away all the type information.

    Test somearrayofTest[999];

    assert((void*)somearrayofTest == (void*)&somearrayofTest);

    The left-hand side of the equality operator is the expression that
    hinges on the fact that the name of the array *decays* to the address of
    the first element of the array. IOW, 'somearrayofTest' is the same as
    (somearrayofTest + 0) or (&(somearrayofTest[0])). The address of the
    array is the same as the address of the first element, except that those
    two pointers have different types. If 'a' is an array of N objects of
    type T, '&a' is a pointer to the array of N objects of T, whereas 'a' is
    a pointer to T.

    When you use 'printf' to examine the value of pointers, you lose a lot:
    the type information is removed, and you have no idea what you're
    looking at. The best approach perhaps is to forget about the fact that
    pointers are addresses in some memory, and in the end are just numbers.
    This is irrelevant to understanding the difference between '&a' and
    'a'. You need to think in abstract terms, like *type* and *object*.

    Good luck!

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, May 28, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    328
  2. Kaz Kylheku

    Re: Can anyone please explain this to me -- thanks

    Kaz Kylheku, Feb 4, 2009, in forum: C Programming
    Replies:
    60
    Views:
    1,081
    CBFalconer
    Feb 10, 2009
  3. John Bode
    Replies:
    0
    Views:
    184
    John Bode
    May 28, 2013
  4. James Kuyper

    Re: confused the address of struct member like this

    James Kuyper, May 28, 2013, in forum: C Programming
    Replies:
    1
    Views:
    204
    Keith Thompson
    May 28, 2013
  5. Malcolm McLean

    Re: confused the address of struct member like this

    Malcolm McLean, May 28, 2013, in forum: C Programming
    Replies:
    0
    Views:
    185
    Malcolm McLean
    May 28, 2013
Loading...

Share This Page