Array name and address of the first element

Discussion in 'C Programming' started by ehames@gmail.com, Jun 28, 2005.

  1. Guest

    Hi guys,

    As far as I know, for any type T, and positive integer N, if I have an
    array declared as:

    T array[N];

    then,

    &array[0] and array are the same element. Is there any reason why a
    programmer should use the first and not the second way to refer to the
    base adress of the first element of the array?

    Kind regards,
    Ed
     
    , Jun 28, 2005
    #1
    1. Advertising

  2. akarl Guest

    wrote:
    > Hi guys,
    >
    > As far as I know, for any type T, and positive integer N, if I have an
    > array declared as:
    >
    > T array[N];
    >
    > then,
    >
    > &array[0] and array are the same element.


    Not the same element, the same *address*.

    > Is there any reason why a
    > programmer should use the first and not the second way to refer to the
    > base adress of the first element of the array?


    Maybe to make it look more familiar to non C programmers.
     
    akarl, Jun 28, 2005
    #2
    1. Advertising

  3. Eric Sosman Guest

    wrote:
    > Hi guys,
    >
    > As far as I know, for any type T, and positive integer N, if I have an
    > array declared as:
    >
    > T array[N];
    >
    > then,
    >
    > &array[0] and array are the same element. Is there any reason why a
    > programmer should use the first and not the second way to refer to the
    > base adress of the first element of the array?


    The two forms are equivalent in almost all contexts
    (the two exceptions: `sizeof &array[0]' does not mean the
    same thing as `sizeof array', and `& array' has a meaning
    while `& &array[0]' does not). In the contexts where the
    two forms mean the same thing to the compiler, the only
    reason to choose one or the other is to help the human
    reader of the code. I'd suggest the following as loose
    guidelines for the choice:

    - If you're "talking about" the array as a whole,
    write `array'. For example, if you're passing the
    array to a function that makes use of its "array-ness"
    (the way qsort() or strlen() do), write `array'.

    - If you're "talking about" one object that just happens
    to reside in an array, `&array[0]' may help emphasize
    the point. For example, you might write something
    like `bsearch(&array[0], values, ...)' to suggest that
    bsearch() will look at all of `values' but only at the
    single key `array[0]'.

    - More generally, I'd suggest writing `array + j' when
    talking about the sub-array from element `j' onward,
    or `&array[j]' when talking about just the one element.

    These are, of course, just stylistic matters -- and matters
    of style have a tendency to become matters of religion, so
    it's likely others will disagree with the way I like to do
    things. I must also admit that I'm not 100% consistent in
    following the practices I recommend ... Take it or leave it.

    --
     
    Eric Sosman, Jun 28, 2005
    #3
  4. Default User Guest

    wrote:
    > Hi guys,
    >
    > As far as I know, for any type T, and positive integer N, if I have an
    > array declared as:
    >
    > T array[N];
    >
    > then,
    >
    > &array[0] and array are the same element.


    There are two exceptions. When the name of an array is used as an
    operand of the sizeof or & operators, it is not converted to a pointer
    to the first element.

    > Is there any reason why a
    > programmer should use the first and not the second way to refer to the
    > base adress of the first element of the array?


    1. Less typing.

    2. It's very idiomatic to C programming.


    If you desire not to use it, don't.


    Brian
     
    Default User, Jun 28, 2005
    #4
  5. Guest

    Default User wrote:
    > wrote:
    > > Hi guys,
    > >
    > > As far as I know, for any type T, and positive integer N, if I have an
    > > array declared as:
    > >
    > > T array[N];
    > >
    > > then,
    > >
    > > &array[0] and array are the same element.

    >
    > > Is there any reason why a
    > > programmer should use the first and not the second way to refer to the
    > > base adress of the first element of the array?

    >
    > 1. Less typing.
    >
    > 2. It's very idiomatic to C programming.
    >
    >
    > If you desire not to use it, don't.


    I actually like using the array name instead of &array[0] but I came
    across some code that uses the second form. I asked the author why it
    was written that way and she replied that some compilers don't
    understand the array name correctly, which sounds rather odd to me.

    Thanks,
    Ed
     
    , Jun 28, 2005
    #5
  6. On 2005-06-28 14:10:24 -0400, "" <> said:

    > Default User wrote:
    >> wrote:
    >>> Hi guys,
    >>>
    >>> As far as I know, for any type T, and positive integer N, if I have an
    >>> array declared as:
    >>>
    >>> T array[N];
    >>>
    >>> then,
    >>>
    >>> &array[0] and array are the same element.

    >>
    >>> Is there any reason why a
    >>> programmer should use the first and not the second way to refer to the
    >>> base adress of the first element of the array?

    >>
    >> 1. Less typing.
    >>
    >> 2. It's very idiomatic to C programming.
    >>
    >>
    >> If you desire not to use it, don't.

    >
    > I actually like using the array name instead of &array[0] but I came
    > across some code that uses the second form. I asked the author why it
    > was written that way and she replied that some compilers don't
    > understand the array name correctly, which sounds rather odd to me.


    I'd bet that it's probably a case of her not understanding when the
    array name decays into a pointer and when it doesn't. If her compiler
    doesn't understand this, then it is not a C compiler


    --
    Clark S. Cox, III
     
    Clark S. Cox III, Jun 28, 2005
    #6
  7. Default User Guest

    wrote:
    > Default User wrote:


    > > If you desire not to use it, don't.

    >
    > I actually like using the array name instead of &array[0] but I came
    > across some code that uses the second form. I asked the author why it
    > was written that way and she replied that some compilers don't
    > understand the array name correctly, which sounds rather odd to me.



    I believe this "author" is badly confused. You could ask for a specific
    example of code and a compiler that didn't handle it correctly. I doubt
    you will get a reasonable answer.

    Most likely, she either ran across one of the exceptions I listed
    before, or she heard it from someone else. Unfortunately, there are a
    number of programmers that don't really understand the language all
    that well.

    I commend you for trying to expand your knowledge and not accepting
    such things at face value.



    Brian
     
    Default User, Jun 28, 2005
    #7
  8. Guest

    Default User wrote:
    > wrote:
    > > Default User wrote:
    > >
    > > I actually like using the array name instead of &array[0] but I came
    > > across some code that uses the second form. I asked the author why it
    > > was written that way and she replied that some compilers don't
    > > understand the array name correctly, which sounds rather odd to me.

    >
    >
    > I believe this "author" is badly confused. You could ask for a specific
    > example of code and a compiler that didn't handle it correctly. I doubt
    > you will get a reasonable answer.
    >


    I already did, and she pointed me to somebody else...

    > Most likely, she either ran across one of the exceptions I listed
    > before, or she heard it from someone else. Unfortunately, there are a
    > number of programmers that don't really understand the language all
    > that well.


    I certainly agree with your last statement.

    > I commend you for trying to expand your knowledge and not accepting
    > such things at face value.


    I keep asking questions the time. I don't accept what I believe should
    be done in a different manner unless someone gives me a reasonable
    proof that I'm wrong.

    Thanks for your comments,
    Ed
     
    , Jun 28, 2005
    #8
  9. In article <d9s1t9$391$>, Eric Sosman <> writes:
    > wrote:
    > >
    > > &array[0] and array are the same element. Is there any reason why a
    > > programmer should use the first and not the second way to refer to the
    > > base adress of the first element of the array?

    >
    > - If you're "talking about" one object that just happens
    > to reside in an array, `&array[0]' may help emphasize
    > the point.


    This often applies when emphasizing parallel code:

    foo(&array[0]);
    bar(&array[1]);
    baz(&array[2]);

    I frequently have to write marshalling code which operates on a list
    of data areas addressed with an array of pointers, which often leads
    to this sort of construction. Using &array[0] for the first element
    keeps it stylistically consistent with the others, clarifying things
    for maintainers.

    --
    Michael Wojcik

    Q: What is the derivation and meaning of the name Erwin?
    A: It is English from the Anglo-Saxon and means Tariff Act of 1909.
    -- Columbus (Ohio) Citizen
     
    Michael Wojcik, Jun 30, 2005
    #9
    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. Luke Wu
    Replies:
    8
    Views:
    463
    Luke Wu
    Jan 18, 2005
  2. Szabolcs Borsanyi
    Replies:
    6
    Views:
    664
    Ben Bacarisse
    May 23, 2008
  3. candide
    Replies:
    65
    Views:
    1,403
  4. John Koleszar

    address of static array element as address constant

    John Koleszar, Jul 7, 2008, in forum: C Programming
    Replies:
    7
    Views:
    624
    John Koleszar
    Jul 15, 2008
  5. Stanley Rice

    Address of array && address of pointer to array

    Stanley Rice, Sep 14, 2011, in forum: C Programming
    Replies:
    33
    Views:
    1,144
    Keith Thompson
    Sep 20, 2011
Loading...

Share This Page