473,385 Members | 1,730 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,385 software developers and data experts.

Commutativity of operator []

Wondering, wondering in hopeless night...

int i;
int sum=0;
int array[5] = {1,2,3,4,5};

for (i=0; i<5; i++) sum += array[i];
for (i=0; i<5; i++) sum += i[array];

I understand why operator [] is commutative, but I'm curious as to
whether anyone has ever found a practical use for the "index[address]"
notation.

FAQ says: "no useful application outside of the Obfuscated C Contest."

What say you?

Thanks!

Jun 23 '06 #1
19 2650
lw*********@hotmail.com wrote:
Wondering, wondering in hopeless night...

int i;
int sum=0;
int array[5] = {1,2,3,4,5};

for (i=0; i<5; i++) sum += array[i];
for (i=0; i<5; i++) sum += i[array];

I understand why operator [] is commutative, but I'm curious as to
whether anyone has ever found a practical use for the "index[address]"
notation.

FAQ says: "no useful application outside of the Obfuscated C Contest."

What say you?


Since the two forms are identical and i[array] can always be written as
array[i] (pedant disclaimer: assuming "array" and "i" are not sinister
macros), the only difference would be readability, I do not think there
are any cases where i[array] would be preferred in sane programming.

Robert Gamble

Jun 23 '06 #2
posted:

I understand why operator [] is commutative, but I'm curious as to
whether anyone has ever found a practical use for the "index[address]"
notation.

You word that as if the "index[address]" notation was formulated for a
specific reason...

I seem it more as a "by-product" of how the C language handles the square
brackets operator. Given the following expression:

a[b]

, it's interpreted EXACTLY as:
*(a + b)

Therefore, you can see how the order doesn't matter -- you could as
easily have written:

*(b + a)
Maybe things get a little fishy though if you start using negative
indexes... not sure.
Jun 23 '06 #3
lw*********@hotmail.com said:
Wondering, wondering in hopeless night...

int i;
int sum=0;
int array[5] = {1,2,3,4,5};

for (i=0; i<5; i++) sum += array[i];
for (i=0; i<5; i++) sum += i[array];

I understand why operator [] is commutative, but I'm curious as to
whether anyone has ever found a practical use for the "index[address]"
notation.

FAQ says: "no useful application outside of the Obfuscated C Contest."

What say you?


This was raised on comp.lang.c a few years ago. Someone or other (I forget
who, I'm afraid) pointed out that if you have a bunch of arrays containing
related data, e.g. char *Name[], int Length[], int Pitch[], int Diameter[],
and, say, int Product, then you might want to write:

DisplayS(hnd, x, y, Product[Name]);
DisplayI(hnd, x, y + 1, Product[Length]);
DisplayI(hnd, x, y + 2, Product[Pitch]);
DisplayI(hnd, x, y + 2, Product[Diameter]);

although why you wouldn't use a struct array in that circumstance is, quite
frankly, beyond me.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 23 '06 #4
Richard Heathfield wrote:
lw*********@hotmail.com said:
Wondering, wondering in hopeless night...

int i;
int sum=0;
int array[5] = {1,2,3,4,5};

for (i=0; i<5; i++) sum += array[i];
for (i=0; i<5; i++) sum += i[array];

I understand why operator [] is commutative, but I'm curious as to
whether anyone has ever found a practical use for the "index[address]"
notation.

FAQ says: "no useful application outside of the Obfuscated C Contest."

What say you?


This was raised on comp.lang.c a few years ago. Someone or other (I forget
who, I'm afraid) pointed out that if you have a bunch of arrays containing
related data, e.g. char *Name[], int Length[], int Pitch[], int Diameter[],
and, say, int Product, then you might want to write:

DisplayS(hnd, x, y, Product[Name]);
DisplayI(hnd, x, y + 1, Product[Length]);
DisplayI(hnd, x, y + 2, Product[Pitch]);
DisplayI(hnd, x, y + 2, Product[Diameter]);

although why you wouldn't use a struct array in that circumstance is, quite
frankly, beyond me.

Organizing the data one way or the other could have a performance impact.
Fondling memory in just the right way is all the rage in optimization land
these days.

Although I still don't think that would warrant cute syntax like this. Let's
all keep in mind it works this way and never, ever speak of it again...

Now, anyone want cookies?

S.
Jun 23 '06 #5
lw*********@hotmail.com wrote:

I understand why operator [] is commutative, but I'm curious as to
whether anyone has ever found a practical use for the "index[address]"
notation.


The only actual use I've ever seen is when "address" was an expression
involving operators with lower precedence than [] and thus would have
needed to be parenthesized when used in the conventional fashion.
Saving two characters of typing is not sufficient justification for the
resulting obfuscation, in my opinion.

-Larry Jones

You can never really enjoy Sundays because in the back of your
mind you know you have to go to school the next day. -- Calvin
Jun 23 '06 #6
lw*********@hotmail.com writes:
int i;
int sum=0;
int array[5] = {1,2,3,4,5};

for (i=0; i<5; i++) sum += array[i];
for (i=0; i<5; i++) sum += i[array];

I understand why operator [] is commutative, but I'm curious as to
whether anyone has ever found a practical use for the "index[address]"
notation.

FAQ says: "no useful application outside of the Obfuscated C Contest."

What say you?


The FAQ is exactly right.

--
Keith Thompson (The_Other_Keith) 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.
Jun 23 '06 #7
Richard Heathfield wrote:
lw*********@hotmail.com said:

.... snip ...

I understand why operator [] is commutative, but I'm curious as
to whether anyone has ever found a practical use for the
"index[address]" notation.

FAQ says: "no useful application outside of the Obfuscated C Contest."

What say you?


This was raised on comp.lang.c a few years ago. Someone or other
(I forget who, I'm afraid) pointed out that if you have a bunch of
arrays containing related data, e.g. char *Name[], int Length[],
int Pitch[], int Diameter[], and, say, int Product, then you might
want to write:

DisplayS(hnd, x, y, Product[Name]);
DisplayI(hnd, x, y + 1, Product[Length]);
DisplayI(hnd, x, y + 2, Product[Pitch]);
DisplayI(hnd, x, y + 2, Product[Diameter]);

although why you wouldn't use a struct array in that circumstance
is, quite frankly, beyond me.


I think the promulgator was me, at least I have been know to
express that opinion.

One reason to use such a format is that the various arrays are
fields in a larger database. This avoids the overhead of accessing
unused fields.

--
Chuck F (cb********@yahoo.com) (cb********@maineline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE maineline address!
Jun 23 '06 #8
On 23 Jun 2006 07:58:00 -0700, in comp.lang.c ,
lw*********@hotmail.com wrote:
I understand why operator [] is commutative, but I'm curious as to
whether anyone has ever found a practical use for the "index[address]"
notation.
I've used it a few times, in cases where it was convenient to have
Index[thing1], Index[thing2] etc etc
FAQ says: "no useful application outside of the Obfuscated C Contest."


It wasnt super-useful, but made the code marginally tidier.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jun 23 '06 #9
Richard Heathfield wrote:
This was raised on comp.lang.c a few years ago. Someone or other (I forget
who, I'm afraid) pointed out that if you have a bunch of arrays containing
related data, e.g. char *Name[], int Length[], int Pitch[], int Diameter[],
and, say, int Product, then you might want to write:

DisplayS(hnd, x, y, Product[Name]);
DisplayI(hnd, x, y + 1, Product[Length]);
DisplayI(hnd, x, y + 2, Product[Pitch]);
DisplayI(hnd, x, y + 2, Product[Diameter]);

although why you wouldn't use a struct array in that circumstance is, quite
frankly, beyond me.


I have no problem with this approach with a bunch of related arrays.
There might be good, valid reasons for laying out the data that way.

However, it's just confusing for later readers of the code, to have to
reverse the array name and the index in the way your code above does.

--
Simon.
Jun 24 '06 #10
Simon Biber said:
Richard Heathfield wrote:
This was raised on comp.lang.c a few years ago. Someone or other (I
forget who, I'm afraid) pointed out that if you have a bunch of arrays
containing related data, e.g. char *Name[], int Length[], int Pitch[],
int Diameter[], and, say, int Product, then you might want to write:

DisplayS(hnd, x, y, Product[Name]);
DisplayI(hnd, x, y + 1, Product[Length]);
DisplayI(hnd, x, y + 2, Product[Pitch]);
DisplayI(hnd, x, y + 2, Product[Diameter]);

although why you wouldn't use a struct array in that circumstance is,
quite frankly, beyond me.


I have no problem with this approach with a bunch of related arrays.
There might be good, valid reasons for laying out the data that way.

However, it's just confusing for later readers of the code, to have to
reverse the array name and the index in the way your code above does.


I agree entirely. I think it's a terrible idea!

(Did you spot the bug, by the way?)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 24 '06 #11
Robert Gamble wrote:

lw*********@hotmail.com wrote:
Wondering, wondering in hopeless night...

int i;
int sum=0;
int array[5] = {1,2,3,4,5};

for (i=0; i<5; i++) sum += array[i];
for (i=0; i<5; i++) sum += i[array];

I understand why operator [] is commutative, but I'm curious as to
whether anyone has ever found a practical use for the "index[address]"
notation.

FAQ says: "no useful application outside of the Obfuscated C Contest."

What say you?


Since the two forms are identical and i[array] can always be written as
array[i] (pedant disclaimer: assuming "array" and "i" are not sinister
macros), the only difference would be readability, I do not think there
are any cases where i[array] would be preferred in sane programming.


Speaking of which, I assume "i++[array]" is valid? (My compiler takes
it, even with warning at max.)

What does "++i[array]" mean? Is it "(++i)[array]" or "++(i[array])"?
(Mine treats it as as the latter. I assume precedence defines it as
such.)

Which leads, of course, to the apparently valid "++i++[array]", which is
the same as "++array[i++]".

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Jun 24 '06 #12
On 2006-06-24, Kenneth Brody <ke******@spamcop.net> wrote:
Robert Gamble wrote:

lw*********@hotmail.com wrote:
> Wondering, wondering in hopeless night...
>
> int i;
> int sum=0;
> int array[5] = {1,2,3,4,5};
>
> for (i=0; i<5; i++) sum += array[i];
> for (i=0; i<5; i++) sum += i[array];
>
> I understand why operator [] is commutative, but I'm curious as to
> whether anyone has ever found a practical use for the "index[address]"
> notation.
>
> FAQ says: "no useful application outside of the Obfuscated C Contest."
>
> What say you?


Since the two forms are identical and i[array] can always be written as
array[i] (pedant disclaimer: assuming "array" and "i" are not sinister
macros), the only difference would be readability, I do not think there
are any cases where i[array] would be preferred in sane programming.


Speaking of which, I assume "i++[array]" is valid? (My compiler takes
it, even with warning at max.)

What does "++i[array]" mean? Is it "(++i)[array]" or "++(i[array])"?
(Mine treats it as as the latter. I assume precedence defines it as
such.)

Which leads, of course, to the apparently valid "++i++[array]", which is
the same as "++array[i++]".


And, since ++i++[array] yields an rvalue, this is also legal:

r[a]+=++i++[a]+-n[a];

where a is an array, and i,r,n are integers.
--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 25 '06 #13
In article <nm************@jones.homeip.net> la************@ugs.com writes:
lw*********@hotmail.com wrote:

I understand why operator [] is commutative, but I'm curious as to
whether anyone has ever found a practical use for the "index[address]"
notation.


The only actual use I've ever seen is when "address" was an expression
involving operators with lower precedence than [] and thus would have
needed to be parenthesized when used in the conventional fashion.
Saving two characters of typing is not sufficient justification for the
resulting obfuscation, in my opinion.


Of course that was not the justification. That arrays are not first-class
citizens in C is the basic reason. I.e., it is just an accident that did
happen. That is, given a pointer (say 's') and an index (say 'i') it was
easier to define 's[i]' as '*(s + i)' without restrictions, than to go to
elaborate rules about the meaning. Moreover, this would work as well if
's' was an array (think about the rule). Commutativity of the addition
did the rest.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jun 26 '06 #14
"Dik T. Winter" <Di********@cwi.nl> writes:
In article <nm************@jones.homeip.net> la************@ugs.com writes:
> lw*********@hotmail.com wrote:
> >
> > I understand why operator [] is commutative, but I'm curious as to
> > whether anyone has ever found a practical use for the "index[address]"
> > notation.

>
> The only actual use I've ever seen is when "address" was an expression
> involving operators with lower precedence than [] and thus would have
> needed to be parenthesized when used in the conventional fashion.
> Saving two characters of typing is not sufficient justification for the
> resulting obfuscation, in my opinion.


Of course that was not the justification. That arrays are not first-class
citizens in C is the basic reason. I.e., it is just an accident that did
happen. That is, given a pointer (say 's') and an index (say 'i') it was
easier to define 's[i]' as '*(s + i)' without restrictions, than to go to
elaborate rules about the meaning. Moreover, this would work as well if
's' was an array (think about the rule). Commutativity of the addition
did the rest.


Agreed.

I'll note that addition didn't necessarily have to be made commutative
in this case. C's "+" operator is overloaded; different forms take
different types of arguments (float, double, int, etc.). The form
that adds a pointer and an integer, yielding a pointer, is a special
case, the only one that takes arguments of different types. If the
language had defined pointer+integer but not integer+pointer, then the
[] operator would not be commutative, and index[array] would be
illegal.

This would, in my humble opinion, have been a better design, but of
course that's not what we have.

--
Keith Thompson (The_Other_Keith) 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.
Jun 26 '06 #15
Richard Heathfield wrote:
Simon Biber said:
>Richard Heathfield wrote:
>>This was raised on comp.lang.c a few years ago. Someone or other (I
forget who, I'm afraid) pointed out that if you have a bunch of arrays
containing related data, e.g. char *Name[], int Length[], int Pitch[],
int Diameter[], and, say, int Product, then you might want to write:

DisplayS(hnd, x, y, Product[Name]);
DisplayI(hnd, x, y + 1, Product[Length]);
DisplayI(hnd, x, y + 2, Product[Pitch]);
DisplayI(hnd, x, y + 2, Product[Diameter]);

although why you wouldn't use a struct array in that circumstance is,
quite frankly, beyond me.
I have no problem with this approach with a bunch of related arrays.
There might be good, valid reasons for laying out the data that way.

However, it's just confusing for later readers of the code, to have to
reverse the array name and the index in the way your code above does.

I agree entirely. I think it's a terrible idea!

(Did you spot the bug, by the way?)
Is it that the pitch and the diameter are displayed on the same line?

--
Simon.

--
Posted via a free Usenet account from http://www.teranews.com

Jul 12 '06 #16
Simon Biber said:
Richard Heathfield wrote:
<snip>
>>
(Did you spot the bug, by the way?)

Is it that the pitch and the diameter are displayed on the same line?
Yes. The copy-paste monster strikes again.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 12 '06 #17
Richard Heathfield wrote:
Simon Biber said:
>Richard Heathfield wrote:
<snip>
>>(Did you spot the bug, by the way?)
Is it that the pitch and the diameter are displayed on the same line?

Yes. The copy-paste monster strikes again.
That was a quick reply: 3 minutes later!

--
Simon.

--
Posted via a free Usenet account from http://www.teranews.com

Jul 12 '06 #18

Keith Thompson wrote:
"Dik T. Winter" <Di********@cwi.nlwrites:
In article <nm************@jones.homeip.netla************@ugs.com writes:
lw*********@hotmail.com wrote:
... That arrays are not first-class
citizens in C is the basic reason....
That is, given a pointer (say 's') and an index (say 'i') it was
easier to define 's[i]' as '*(s + i)' without restrictions, than to go to
elaborate rules about the meaning. Moreover, this would work as well if
's' was an array (think about the rule). Commutativity of the addition
did the rest.
Yes, C would lose much elegance if arrays were "first class" as
is so often proposed. (And if you need an array to be first-class,
you can simply enclose it within "struct { }" )
... If the
language had defined pointer+integer but not integer+pointer, then the
[] operator would not be commutative, and index[array] would be
illegal.
What would be the purpose (beyond outlawing a "cute" expression)?
It might create confusion for parsers and macro-writers.
(ptr + int + ptr - ptr) is not unheard of (e.g. we might have just
run index() on one of two similar strings) and you'd be asking us
to worry about associativity.
This would, in my humble opinion, have been a better design, but of
course that's not what we have.
Keith and I have agreed to disagree on C's handling of pointers.
For me, it's the feature that makes C distinctly better and more
elegant
than Pascal, etc.

OP wrote:
>FAQ says: "no useful application outside of the Obfuscated C Contest."
What say you?
The FAQ is exactly right.
To put the meaningful variable first,
I sometimes write something like
printf("I move my %c", /* tell him which piece we moved */
moved_piece[type]["KQRBNP"]);

If this is "less readable" it is only because of a prejudice that a[b]
isn't
commutative.
Keith Thompson ...
We must do something. This is something. Therefore, we must do this.
Nothing is better than something like this.

James Dow Allen

Jul 12 '06 #19
James Dow Allen wrote:
(ptr + int + ptr - ptr) is not unheard of
As in "I heard the compiler correctly reported an error for this
expression".

--
Chris "I'M NOT DEAF" Dollin
"Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/

Jul 12 '06 #20

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

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
14
by: lutorm | last post by:
Hi everyone, I'm trying to use istream_iterators to read a file consisting of pairs of numbers. To do this, I wrote the following: #include <fstream> #include <vector> #include <iterator> ...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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
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...

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.