Is it the same?

Discussion in 'C Programming' started by QQ, Jul 7, 2005.

  1. QQ

    QQ Guest

    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!
     
    QQ, Jul 7, 2005
    #1
    1. Advertising

  2. QQ

    Richard Bos Guest

    "QQ" <> 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
     
    Richard Bos, Jul 7, 2005
    #2
    1. Advertising

  3. 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, Jul 7, 2005
    #3
  4. 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
    > 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.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Jul 7, 2005
    #4
    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. David Hubbard
    Replies:
    2
    Views:
    835
    David Hubbard
    Jan 12, 2006
  2. Shawn
    Replies:
    2
    Views:
    377
    =?Utf-8?B?RWx0b24gVw==?=
    Feb 19, 2006
  3. =?Utf-8?B?UHVuaXNoZXI=?=

    Need same viewstate for same page across multiple servers

    =?Utf-8?B?UHVuaXNoZXI=?=, Mar 23, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    416
    John Timney \( MVP \)
    Mar 23, 2006
  4. darrel
    Replies:
    2
    Views:
    472
    darrel
    Apr 5, 2006
  5. Krick
    Replies:
    1
    Views:
    1,464
    John C. Bollinger
    Aug 15, 2003
Loading...

Share This Page