Re: confused the address of struct member like this

Discussion in 'C Programming' started by John Bode, May 28, 2013.

  1. John Bode

    John Bode Guest

    On May 28, 4:58 am, Ed <> wrote:
    > confused the address of struct member like this
    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > 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;}
    >
    > ===========================================
    > (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 are same in (2) and (3)?
    >
    > pObj->Data == &pObj->Data?
    >
    > --- news://freenews.netfront.net/ - complaints: ---


    Both expressions have the same *value*, but they have different types.

    "&pObj->Data" evaluates to the address of the "Data" array in the
    struct pointed to by pObj, and its type is "pointer to 32-element
    array of char", or "char (*)[32]".

    Except when it is the operand of the sizeof or unary & operator, or is
    a string literal being used to initialize an array in a declaration,
    an expression of type "N-element array of T" will be converted to an
    expression of type "pointer to T", and its value will be the address
    of the first element in the array. Thus, the expression "pObj->Data"
    will be converted from an expression of type "32-element array of
    char" to an expression of type "pointer to char", or "char *", and its
    value will be the address of the first element in the array.

    Since the address of the first element in an array is the same as the
    address of the array itself, both expressions will result in the same
    value; it's just that the types will be different.
     
    John Bode, 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. SG
    Replies:
    0
    Views:
    196
  2. James Kuyper

    Re: confused the address of struct member like this

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

    Re: confused the address of struct member like this

    Malcolm McLean, May 28, 2013, in forum: C Programming
    Replies:
    0
    Views:
    194
    Malcolm McLean
    May 28, 2013
  4. Victor Bazarov
    Replies:
    0
    Views:
    189
    Victor Bazarov
    May 28, 2013
  5. Victor Bazarov
    Replies:
    3
    Views:
    193
    Jorgen Grahn
    May 29, 2013
Loading...

Share This Page