Simple array question...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • main()

    #1

    Simple array question...

    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.

  • Richard Heathfield

    #2
    Re: Simple array question...

    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[i] == *(a + i)

    Therefore, if we take addresses of either side:

    &a[i] == &*(a + i)

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

    &a[i] == (a + i)

    Removing superfluous parentheses:

    &a[i] == 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

    email: rjh at above domain (but drop the www, obviously)

    Comment

    • Joe Wright

      #3
      Re: Simple array question...

      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[i] == *(a + i)
      >
      Therefore, if we take addresses of either side:
      >
      &a[i] == &*(a + i)
      >
      Now, & and * are inverses of each other, so they cancel on the RHS:
      >
      &a[i] == (a + i)
      >
      Removing superfluous parentheses:
      >
      &a[i] == 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 ---

      Comment

      Working...