473,657 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2368
eh****@gmail.co m 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.co m 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.co m 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.co m 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.c om" <eh****@gmail.c om> said:
Default User wrote:
eh****@gmail.co m 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.co m 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.co m 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**********@n ews1brm.Central .Sun.COM>, Eric Sosman <er*********@su n.com> writes:
eh****@gmail.co m 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
10121
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 code... TCHAR myArray; DoStuff(myArray);
22
4604
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 month I had enough of extra proof that the cat doesn't hount mice anymore in more and more situations. And the surrent sicretisme among array and hash is the base for it. I summarized all points in this article:...
9
2309
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
2432
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: Why cannot I do the following:
1
617
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 platform which has the following properties: 1) char's are 8-Bit. ( "char" is synomonous with "byte" ). 2) int's are 32-Bit. ( sizeof(int) == 4 ). 3) Pointers are 64-Bit. ( sizeof(int*) == 8 ).
12
3872
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 looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
10
1760
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 very simple, does anyone know what is wrong with the program?
7
3199
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, int n);
4
1643
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
2312
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 same as its address. The second one simply depends on a term that is not well-defined. Most people consider the type to be an important part of the notion of
0
8399
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8312
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8606
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4159
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2732
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.