473,847 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inside-Out Array-Indexing Syntax

Greetings everyone,

I have noticed a little quirk in C's array-indexing syntax.
I wonder what practical purpose this little "inverting" serves
to be included in the standard (it compiles fine with
gcc -pedantic -Wall -std=c89 and executes properly when run).
The code follows:

#include <stdio.h>

#define A_MAX (5)

int
main (int argc, char ** argv)
{
int a[A_MAX] = {0, 1, 2, 3, 4};
int i;

for (i = 0; i < A_MAX; ++i)
printf ("%d\n", i[a]); /* <<-- inside-out */

return 0;
}

Any help will be appreciated.

Regards,
Jonathan.

--
"If unsigned integers look like two's complement (signed) integers,
it's because two's complement integers look like unsigned integers."
-Peter Nilsson
Nov 14 '05 #1
9 1782
Jonathan Burd wrote:
[...]

#include <stdio.h>

#define A_MAX (5)

[...]
Oops. Named it incorrectly. My bad. A_MAXELEMENTS.
[...]

Regards,
Jonathan.

--
"If unsigned integers look like two's complement (signed) integers,
it's because two's complement integers look like unsigned integers."
-Peter Nilsson
Nov 14 '05 #2
Jonathan Burd <jo***********@ gmail.com> wrote:
Greetings everyone,

I have noticed a little quirk in C's array-indexing syntax.
I wonder what practical purpose this little "inverting" serves
to be included in the standard (it compiles fine with
gcc -pedantic -Wall -std=c89 and executes properly when run).
The code follows:

#include <stdio.h>

#define A_MAX (5)

int
main (int argc, char ** argv)
{
int a[A_MAX] = {0, 1, 2, 3, 4};
int i;

for (i = 0; i < A_MAX; ++i)
printf ("%d\n", i[a]); /* <<-- inside-out */

return 0;
}

Any help will be appreciated.


It is not quite clear what you are asking about, but I guess you are
wondering why a[i] and i[a] are equivalent.
It is because a[i] is just syntactic sugar for (*((a)+(i))) and x+y and
y+x are of course equivalent. Thus you get:

a[i] == (*((a)+(i))) == (*((i)+(a))) == i[a]

--
<Insert your favourite quote here.>
Erik Trulsson
er******@studen t.uu.se
Nov 14 '05 #3
Erik Trulsson wrote:
Jonathan Burd <jo***********@ gmail.com> wrote:
Greetings everyone,

I have noticed a little quirk in C's array-indexing syntax.
I wonder what practical purpose this little "inverting" serves
to be included in the standard (it compiles fine with
gcc -pedantic -Wall -std=c89 and executes properly when run).
The code follows:

#include <stdio.h>

#define A_MAX (5)

int
main (int argc, char ** argv)
{
int a[A_MAX] = {0, 1, 2, 3, 4};
int i;

for (i = 0; i < A_MAX; ++i)
printf ("%d\n", i[a]); /* <<-- inside-out */

return 0;
}

Any help will be appreciated.

It is not quite clear what you are asking about, but I guess you are
wondering why a[i] and i[a] are equivalent.
It is because a[i] is just syntactic sugar for (*((a)+(i))) and x+y and
y+x are of course equivalent. Thus you get:

a[i] == (*((a)+(i))) == (*((i)+(a))) == i[a]


Yes, I meant why a[i] and i[a] are equivalent. Thanks. :)

Regards,
Jonathan.

--
"If unsigned integers look like two's complement (signed) integers,
it's because two's complement integers look like unsigned integers."
-Peter Nilsson
Nov 14 '05 #4
On Mon, 17 Jan 2005 17:46:33 +0530, Jonathan Burd wrote:
Erik Trulsson wrote:


....
It is not quite clear what you are asking about, but I guess you are
wondering why a[i] and i[a] are equivalent.
It is because a[i] is just syntactic sugar for (*((a)+(i))) and x+y and
y+x are of course equivalent. Thus you get:

a[i] == (*((a)+(i))) == (*((i)+(a))) == i[a]


The standard says of the [] operator

"One of the expressions shall have type ''pointer to object
type'', the other expression shall have integer type, and the result
has type ''type''."

It COULD have required the first expression to be a pointer and the second
an integer, and perhaps it should have. However C seems to have developed
historically allowing both orderings and that existing practice was
standardised.

Lawrence

Nov 14 '05 #5
Jonathan Burd wrote:
I wonder what practical purpose this little "inverting" serves
to be included in the standard (it compiles fine with
gcc -pedantic -Wall -std=c89 and executes properly when run).


It is not "included in the standard". It is simply "not excluded" from
the standard. This is nothing more than an innocent side effect of the
way the [] operator is defined in C (see other replies for more detailed
explanation).

Prohibiting this syntax would take extra effort. There is simply no
point in eliminating this harmless quirk of the language syntax.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #6

"Andrey Tarasevich" <an************ **@hotmail.com> wrote in message
news:10******** *****@news.supe rnews.com...
Jonathan Burd wrote:
I wonder what practical purpose this little "inverting" serves
to be included in the standard (it compiles fine with
gcc -pedantic -Wall -std=c89 and executes properly when run).


It is not "included in the standard". It is simply "not excluded" from
the standard. This is nothing more than an innocent side effect of the
way the [] operator is defined in C (see other replies for more detailed
explanation).

Prohibiting this syntax would take extra effort. There is simply no
point in eliminating this harmless quirk of the language syntax.


But unless one is writing 'obfuscated code' intentionally, one
should prefer the more intutitive and readable form:

array_or_pointe r_name[index_value]

-Mike
Nov 14 '05 #7
Jonathan Burd <jo***********@ gmail.com> writes:
Greetings everyone,

I have noticed a little quirk in C's array-indexing syntax.
I wonder what practical purpose this little "inverting" serves
to be included in the standard (it compiles fine with
gcc -pedantic -Wall -std=c89 and executes properly when run).
The code follows: [snip] printf ("%d\n", i[a]); /* <<-- inside-out */

[snip]

This is question 6.11 in the C FAQ,
<http://www.eskimo.com/~scs/C-faq/faq.html>.

I've argued in the past that making pointer addition non-commutative
(allowing pointer+integer but banning integer+pointer ) would have been
a good idea; it would have eliminated silly things like i[a] without
causing any real problems. But of course it can't be changed now
without breaking existing code (and it's not that big a deal anyway).
I wouldn't mind breaking i[a], but requiring all occurrences of
integer+pointer to be changed to pointer+integer could be a problem.

(If you disagree with me, you're in good company; plenty of people did
so when I suggested it before.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
Mike Wahler wrote:
"Andrey Tarasevich" <an************ **@hotmail.com> wrote in message

.... snip on a[i] vs i[a] ...

Prohibiting this syntax would take extra effort. There is simply no
point in eliminating this harmless quirk of the language syntax.


But unless one is writing 'obfuscated code' intentionally, one
should prefer the more intutitive and readable form:

array_or_pointe r_name[index_value]


There are cases when the so-called inverted notation is clearer.
For example, a database where the various fields are stored in
individual arrays, and the data base item is the actual index. Now
we may use item[fieldname] analagous with accessing a record named
item by item.fieldname.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #9
On Mon, 17 Jan 2005 11:30:25 -0800, Andrey Tarasevich wrote:
Jonathan Burd wrote:
I wonder what practical purpose this little "inverting" serves
to be included in the standard (it compiles fine with
gcc -pedantic -Wall -std=c89 and executes properly when run).
It is not "included in the standard". It is simply "not excluded" from
the standard.


A distinction without a difference. The actual text of the standard is

"One of the expressions shall have type ''pointer to object type'', the
other expression shall have integer type, and the result has type
''type''."

The choice is explicit in the phrase "One of the expressions" and could be
"fixed" by shortening it to "The first expression".
This is nothing more than an innocent side effect of the
way the [] operator is defined in C (see other replies for more detailed
explanation).
There's nothing innocent about it. The wording of the standard is clearly
crafted to allow this possibility, it isn't something that just leaked
through by accident.
Prohibiting this syntax would take extra effort.
Not in the standard, probably not in a compiler. If it did require extra
effort it would be minimal. The standard is as it is because that was the
existing practice at the time. It was existing practice probably because
it seemed like a good idea at the time.
There is simply no
point in eliminating this harmless quirk of the language syntax.


Not now anyway.

Lawrence

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3756
by: Anders Dalvander | last post by:
os.popen does not work with parameters inside quotes, nor do os.popen. At least on Windows. import os cmd = '"c:\\command.exe" "parameter inside quotes"' os.popen4(cmd) Results in the following error message: 'c:\\command.exe" "parameter inside quotes' is not recognized as an
12
6909
by: Thanasis \(sch\) | last post by:
Hello to everyone, i'm new to css and i have a question. i have an external css (style.css) , that i link in all html pages. inside the css file there is a class detais declared as follows: *=========================================================== */ ..details { font-family: tahoma, verdana, arial, helvetica; font-size:11pt; color:#051E78;
15
1887
by: Falc2199 | last post by:
Is there any limitations as to what can be inside <div id = "" > </div> ? Can you place TDs and TRs for table inside it? Thanks, Jehan
0
1459
by: Severino | last post by:
Hi all, we have developed a .NET component for use inside Windows Forms: this component has been written using VC++.NET (2003) and is working perfectly when inserted inside VC#.NET or VB.NET projects; its generated assembly is inside MyAssembly.dll When used inside a VC++.NET project it appears correctly over forms at design time but when trying to compile the project we get the following error:
0
1333
by: Severino | last post by:
Hi all, we have developed a .NET component for use inside Windows Forms: this component has been written using VC++.NET (2003) and is working perfectly when inserted inside VC#.NET or VB.NET projects; its generated assembly is inside MyAssembly.dll When used inside a VC++.NET project it appears correctly over forms at design time but when trying to compile the project we get the following error:
0
1654
by: Les Caudle | last post by:
I have a menu system composed of a DataList nested inside a DataList. The outer DataList has it's DataSource (composed of a DataSet with two tables linked by a CategoryPagesRelation Relation) set in the Page_Load. The inner DataList has its DataSource set in the ascx file as: <asp:DataList ID="PageList" runat="server" CellPadding="3" CellSpacing="0" DataSource='<%#...
1
8347
by: Matik | last post by:
Hi to all, Probably I'm just doing something stupid, but I would like you to tell me that (if it is so), and point the solution. There ist the thing: I' having a sp, where I call other sp inside. The only problem is, the name of this inside sp is builded variously, and executed over sp_executesql:
4
1986
by: Rares Vernica | last post by:
Hi, How can I save a reference inside a container? For example I have: map<string, unsignedX; I would like to be able to save a reference to a position inside X. For a vector, the reference would be the index inside the vector. For
5
3641
by: Test | last post by:
Is it possibel to have DIV's inside a table cell so that their position can be given relative to top left corner of the cell. Now the DIVs seem to position themselves relative to previous object.
1
10718
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
10347
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
9490
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7888
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7061
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
5725
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...
0
5915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4540
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
4129
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.