Array name and address of the first element

E

ehames

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
 
A

akarl

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.
 
E

Eric Sosman

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.
 
D

Default User

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
 
E

ehames

Default said:
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
 
C

Clark S. Cox III

Default said:
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
 
D

Default User

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
 
E

ehames

Default said:
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
 
M

Michael Wojcik

&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 (e-mail address removed)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top