469,631 Members | 1,515 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

Array name and address of the first element

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

Nov 15 '05 #1
8 2065
eh****@gmail.com 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.
Nov 15 '05 #2


eh****@gmail.com 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.

--
Er*********@sun.com

Nov 15 '05 #3


eh****@gmail.com 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

Nov 15 '05 #4
Default User wrote:
eh****@gmail.com 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

Nov 15 '05 #5
On 2005-06-28 14:10:24 -0400, "eh****@gmail.com" <eh****@gmail.com> said:
Default User wrote:
eh****@gmail.com 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
cl*******@gmail.com

Nov 15 '05 #6


eh****@gmail.com 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

Nov 15 '05 #7
Default User wrote:
eh****@gmail.com 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

Nov 15 '05 #8

In article <d9**********@news1brm.Central.Sun.COM>, Eric Sosman <er*********@sun.com> writes:
eh****@gmail.com 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 mi************@microfocus.com

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
Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Tomás | last post: by
10 posts views Thread by =?Utf-8?B?R2Vvcmdl?= | last post: by
7 posts views Thread by lovecreatesbea... | last post: by
4 posts views Thread by nembo kid | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.