   [C] "Reverse-indexing" language oddity
  Hello.A friend of mine checked some source code of an obfuscated code contest and discovered a very interesting "feature" of the C language which, by lack of a better term, I'll call "reverse-indexing". It goes like that :#include <stdio.h>int main(){printf("%c", 6["Hello World!"]); /* prints 'W' */}This compiles and runs fine under Visual C++ (in C or C++ mode). I really fail to understand why such a "feature" exists, and what advantages it has over normal, sane indexing. Of all C/C++ oddities, this must be one of the worst.Any ideas on why that "feature" exists? I'm really curious to know.   
Quote:Original post by TrillianOf all C/C++ oddities, this must be one of the worst.I'm simply speechless. Tried it with other array types and same (working) results.   ;  I don't actually know but I suspect that it's a holdover from the early days when C didn't have data types, just a single general 16-bit word type representing both integers and addresses. In other words the compiler didn't know or care whether a given variable represented an integer or a pointer, the only thing that mattered was how you treated it.If you look closely there's a few other artifacts left from those early days. Like the auto keyword, or the precedence of the & and | operators.   ;Quote:Of all C/C++ oddities, this must be one of the worst.Uh, have you ever used C++? There are MUCH worse things than this. [grin]Although I agree, it's a stupid decision to make that legal.   ;  @implicitThat's cool to know, I never knew that the C language was that basic at the beginning.@Drew_BentonHaha yes I've used C++, I tried to be careful not to say "the worst" because I know there's a lot of weird stuff in that language. However I still think it is one of the weirdest I've seen (that and the goto-label address stuff that gcc authorizes).Edit: BTW it works with variables too, allowing stuff like :void printChars(const char * text){int i = 0;while (i[text]) printf("%c", i++[text]);};Quote:Any ideas on why that "feature" exists? I'm really curious to know.It is an artifact of the fact that, for pointer types, the expression a[n] is actually *(a + n) -- given the simple algebraic properties of addition, you can see that *(a + n) == *(n + a) == n[a] == a[n]I don't know why they initially did not special-case the scenario where the outer operand is a literal or nonpointer with no operator[] overload (in C++) -- perhaps due to what implicit said. But the C and C++ committee's rarely remove features that could damage backwards compatibility, so that's a likely explaination as to why its still around.   ;Quote:Original post by jpetrieIt is an artifact of the fact that, for pointer types, the expression a[n] is actually *(a + n) -- given the simple algebraic properties of addition, you can see that *(a + n) == *(n + a) == n[a] == a[n]Oh I see, it kinda makes sense :S... mathematically at least. Thanks for clearing that up.   ;Quote:Original post by jpetrieGreat explanation, thanks!At first, after reading your post, I didn't like the idea, but after thinking about it some more, in the end, it does make sense. Rather than accessing base memory address + offset, you are accessing offset + base memory address, which as you showed with the math, is the same. What I was thinking about though, was the pointer addition rules, but it would still always point to the correct address. What if the index was a smaller data type, like 2 bytes, then the pointer would have to be off, but that's not the case due to the pointer addition rules. The index isn't taken as a 'pointer', the pointer type is (the compiler has it figured out. I tried using variables in place of the constant too) Good entertainment for the night [lol]   ;Quote:Original post by Mike.PopoloskiQuote:Of all C/C++ oddities, this must be one of the worst.Uh, have you ever used C++? There are MUCH worse things than this. [grin]Although I agree, it's a stupid decision to make that legal.This, from the guy who likes to abuse the comma operator?;Quote:Original post by jpetrieQuote:Original post by Mike.PopoloskiQuote:Of all C/C++ oddities, this must be one of the worst.Uh, have you ever used C++? There are MUCH worse things than this. [grin]Although I agree, it's a stupid decision to make that legal.This, from the guy who likes to abuse the comma operator?It's not abuse! All the cool kids are doing it. Besides, it's completely in keeping with the spirit of C++ [grin]std::cout << "Enter desired motor speed between 1.5 and 8.5: ";while (std::cin >> desiredSpeed, desiredSpeed < 1.5 || desiredSpeed > 8.5)    std::cout << "Invalid speed value. Please enter a speed between 1.5 and 8.5: ";Elegant, am I right?   