Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: I confused Address of struct member like this, please explain,thanks

Reply
Thread Tools

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

 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-28-2013
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Who can explain this bug? mathog C Programming 57 06-11-2013 10:09 PM
Re: confused the address of struct member like this James Kuyper C Programming 1 05-28-2013 03:01 PM
Re: confused the address of struct member like this Malcolm McLean C Programming 0 05-28-2013 12:47 PM
Re: confused the address of struct member like this John Bode C Programming 0 05-28-2013 10:41 AM
This looks like a Perl bug George Mpouras Perl Misc 18 04-21-2013 11:56 PM



Advertisments