Velocity Reviews > Is it the same?

# Is it the same?

QQ
Guest
Posts: n/a

 07-07-2005
For instance I have a struct

struct A{
char ID[10];
}A;

when I call the ID in struct A

A aa;
is &aa.ID[0] and aa.ID
the same?

Thanks a lot!

Richard Bos
Guest
Posts: n/a

 07-07-2005
"QQ" <(E-Mail Removed)> wrote:

> For instance I have a struct
>
> struct A{
> char ID[10];
> }A;
>
> when I call the ID in struct A

You do not call an array. You call a function.

> A aa;
> is &aa.ID[0] and aa.ID
> the same?

Depends on the context. Usually, yes, they are the same (or more
precisely, aa.ID is evaluated as if it were the same as &aa.ID[0]).
But for example, as the operand of sizeof, they're not the same: sizeof
&aa.ID[0] is equal to sizeof (char *), while sizeof aa.ID is equal to
10*sizeof char, i.e., to 10.

Richard

Andrey Tarasevich
Guest
Posts: n/a

 07-07-2005
QQ wrote:
> For instance I have a struct
>
> struct A{
> char ID[10];
> }A;
>
> when I call the ID in struct A
>
> A aa;
> is &aa.ID[0] and aa.ID
> the same?
>

Formally speaking, no. One example where thse two behave differently was already
mentioned by Richard.

Also one can note that the former is an lvalue, while the latter is an rvalue.
There are contexts where this will make a difference. For example, you can apply
an 'address-of' operator to 'aa.ID' and get the pointer to the entire array
(pointer of type 'char (*)[10]')

char (*ptr)[10 = &aa.ID;

However, it is worth mentioning that in virtually all other contexts these two
variants will behave identically.

--
Best regards,
Andrey Tarasevich

Andrey Tarasevich
Guest
Posts: n/a

 07-07-2005
Andrey Tarasevich wrote:

> QQ wrote:
>
>> For instance I have a struct
>>
>> struct A{
>> char ID[10];
>> }A;
>>
>> when I call the ID in struct A
>>
>> A aa;
>> is &aa.ID[0] and aa.ID
>> the same?
>>

>
> Formally speaking, no. One example where thse two behave differently was
>
> Also one can note that the former is an lvalue, while the latter is an
> rvalue.

Sorry, should be other way around.

--
Best regards,
Andrey Tarasevich