473,411 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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 2345
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
9
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
28
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions...
1
by: Tomás | last post by:
Some programmers treat arrays just like pointers (and some even think that they're exactly equivalent). I'm going to demonstrate the differences. Firstly, let's assume that we're working on a...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
10
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, There is error message when executing my program, Unhandled exception at 0x00411a49 in test_entern.exe: 0xC0000005: Access violation reading location 0x00000002. It is...
7
by: lovecreatesbea... | last post by:
Is it always legal to cast expressions of type of multi-dimension array to type of pointer? Including: T to T* , T to T* , T to T* , and so on... For example: int *mtxrot1d(int *p,...
4
by: nembo kid | last post by:
I have the following bidimensional array int a ; Why the first address of this array is only: & (mat) and not also:
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.