Simple array question...

Discussion in 'C Programming' started by main(), Aug 7, 2006.

  1. main()

    main() Guest

    Hi all,

    If 'a' is an array of ten characters , say

    char a[10];

    whenever i say 'a' in my code it means &a[0]. (except in sizeof
    operator).
    Then why is &a is same as &a[0] and not &(&a[0]) ?

    Thanks for your time.
     
    main(), Aug 7, 2006
    #1
    1. Advertising

  2. main() said:

    > Hi all,
    >
    > If 'a' is an array of ten characters , say
    >
    > char a[10];
    >
    > whenever i say 'a' in my code it means &a[0]. (except in sizeof
    > operator).


    And except when it is the operand of the & operator, and except when you are
    defining it - in fact, except any situation except the situation where you
    are evaluating the array.

    > Then why is &a is same as &a[0] and not &(&a[0]) ?


    But it isn't! &a has the type "pointer to array of char[10]", whereas &a[0]
    has the type "pointer to char". Since they have different types, it's hard
    to argue that they are "the same".

    The thing to bear in mind is this: when you're using an array name in a
    value context (e.g. a lookup, an assignment, whatever), the Standard
    guarantees the following equivalence:

    a == *(a + i)

    Therefore, if we take addresses of either side:

    &a == &*(a + i)

    Now, & and * are inverses of each other, so they cancel on the RHS:

    &a == (a + i)

    Removing superfluous parentheses:

    &a == a + i

    Setting i to 0:

    &a[0] == a + 0

    Addition of 0 is redundant:

    &a[0] == a

    See? :)

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Aug 7, 2006
    #2
    1. Advertising

  3. main()

    Joe Wright Guest

    Richard Heathfield wrote:
    > main() said:
    >
    >> Hi all,
    >>
    >> If 'a' is an array of ten characters , say
    >>
    >> char a[10];
    >>
    >> whenever i say 'a' in my code it means &a[0]. (except in sizeof
    >> operator).

    >
    > And except when it is the operand of the & operator, and except when you are
    > defining it - in fact, except any situation except the situation where you
    > are evaluating the array.
    >
    >> Then why is &a is same as &a[0] and not &(&a[0]) ?

    >
    > But it isn't! &a has the type "pointer to array of char[10]", whereas &a[0]
    > has the type "pointer to char". Since they have different types, it's hard
    > to argue that they are "the same".
    >
    > The thing to bear in mind is this: when you're using an array name in a
    > value context (e.g. a lookup, an assignment, whatever), the Standard
    > guarantees the following equivalence:
    >
    > a == *(a + i)
    >
    > Therefore, if we take addresses of either side:
    >
    > &a == &*(a + i)
    >
    > Now, & and * are inverses of each other, so they cancel on the RHS:
    >
    > &a == (a + i)
    >
    > Removing superfluous parentheses:
    >
    > &a == a + i
    >
    > Setting i to 0:
    >
    > &a[0] == a + 0
    >
    > Addition of 0 is redundant:
    >
    > &a[0] == a
    >
    > See? :)
    >

    Priceless.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Aug 7, 2006
    #3
    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. Replies:
    0
    Views:
    580
  2. Kevin Spencer

    Re: Simple Simple question!!!

    Kevin Spencer, Jun 25, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    687
    Kevin Spencer
    Jun 25, 2004
  3. Daniel Frey

    Simple Question - Simple Answer?

    Daniel Frey, Dec 28, 2004, in forum: XML
    Replies:
    4
    Views:
    860
    Daniel Frey
    Jan 12, 2005
  4. Oli

    simple simple question

    Oli, Jan 26, 2004, in forum: ASP General
    Replies:
    10
    Views:
    387
    Roland Hall
    Jan 26, 2004
  5. Peter Bailey

    simple, simple array question

    Peter Bailey, Apr 8, 2008, in forum: Ruby
    Replies:
    7
    Views:
    233
    Peter Bailey
    Apr 8, 2008
Loading...

Share This Page