Is it the same?

Q

QQ

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!
 
R

Richard Bos

QQ said:
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
 
A

Andrey Tarasevich

QQ said:
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.
 
A

Andrey Tarasevich

Andrey said:
QQ said:
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.

Sorry, should be other way around.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top