446,159 Members | 913 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

# Commutativity of operator []

 P: n/a 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 Replies

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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 San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Jun 23 '06 #7

 P: n/a 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. USE maineline address! Jun 23 '06 #8

 P: n/a 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 towhether 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

 P: n/a 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

 P: n/a 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

 P: n/a 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 | | +-------------------------+--------------------+-----------------------+ Don't e-mail me at: Jun 24 '06 #12

 P: n/a On 2006-06-24, Kenneth Brody 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

 P: n/a In article 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

 P: n/a "Dik T. Winter" writes: In article 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 San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Jun 26 '06 #15

 P: n/a Richard Heathfield wrote: Simon Biber said: >Richard Heathfield wrote: >>This was raised on comp.lang.c a few years ago. Someone or other (Iforget who, I'm afraid) pointed out that if you have a bunch of arrayscontaining 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 toreverse 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

 P: n/a Simon Biber said: Richard Heathfield wrote: >>(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

 P: n/a Richard Heathfield wrote: Simon Biber said: >Richard Heathfield wrote: >>(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

 P: n/a Keith Thompson wrote: "Dik T. Winter" 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

 P: n/a 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 discussion thread is closed

Replies have been disabled for this discussion.