News for hackers. Stuff that matters.
Re: Strangest language feature by edanm on Sunday March 24, 2013 @03:31PM (#5433206)
The closest I am to being famous is this question on SO, to which I gave the #1 answer. I'd been waiting years to get that piece of trivia off my chest, and it was oh-so-rewarding. The answer was that you can use arrays like so: 5[arr], not just the usual way (arr[5])

Funny enough, several people gave interesting places where this kind of syntax is a good thing. Goes to show that even meaningless pieces of fun on SO still end of enriching the reader.

Re: Strangest language feature by reeses on Sunday March 24, 2013 @04:02PM (#5433342)
Pointer arithmetic via syntactic sugar is definitely fun. I still love the occasional "variable index of a literal".
Re: Strangest language feature by mikeash on Sunday March 24, 2013 @05:52PM (#5433875)
What places is this syntax a good thing? I read through the comments on your answer, but every single one that claimed this was either pointless (e.g. 0[x] instead of (x)[0]) or not even an example of the trick in question (e.g. "blah"[index] is not this). I was hoping for something interesting, thus my question.
Re: Strangest language feature by edanm on Sunday March 24, 2013 @06:04PM (#5433947)
Honestly, I wrote this because I remembered that somebody had a use for this. A funny use, but a semi-legitimate one none the less.

Unfortunately, I can't really find it now - there is a link to a question which claims to have a use for this syntax, but it's been removed.

There are these 2 comments: "Don't forget "Hello World"[i]. Or i["Hello World"] – Richard Pennington Jan 3 '10 at 15:12"

"Or, more usefully, "0123456789abcdef"[x & 0xf] "

Their basic point is that you can use this trick to find the i'th character in a string. Which I guess is useful sometimes...

Re: Strangest language feature by derobert on Monday March 25, 2013 @02:28PM (#5438693)
The answer on the deleted question (http://stackoverflow.com/questions/469696/what-is-your-most-...), by Josh Kelley, gives this:

---

Get the number of elements in an array:

    #define ITEMSOF(arr)    (sizeof(arr) / sizeof(0[arr]))
(0[arr] is identical to arr[0] for arrays but will intentionally fail if it's used against a C++ object that overloads operator[].)

He gives a C++ template version, too.

---

... 10k+ users on Stack Overflow can see deleted stuff. Or you can probably get them out of the CC data dumps (but that's a lot more work than you want, probably). Or maybe out of the data explorer.

Re: Strangest language feature by mikeash on Monday March 25, 2013 @02:40PM (#5438775)
Ah! I saw that #define but didn't see the bit about failing on C++ objects that overload operator[]. I don't do C++ much so that really didn't occur to me.

I personally prefer sizeof(*(arr)), which is the same as arr[0], but I suppose C++ lets you overload that as well.

Re: Strangest language feature by mikeash on Sunday March 24, 2013 @06:09PM (#5433983)
Except that "0123456789abcdef"[x & 0xf] is just normal array dereferencing syntax, not the backwards kind you pointed out. The backwards version would be (x & 0xf)["0123456789abcdef"], which isn't really useful, I think.

Oh well. If you happen to find the funny but semi-legitimate one, I'd love to see it.

Re: Strangest language feature by greyfade on Tuesday March 26, 2013 @06:11PM (#5446400)
It was used in one of the early winners of the IOCCC: http://www.ioccc.org/1984/anonymous.c

It's rumored to have been written by either Kernighan or Ritchie.

Re: Strangest language feature by edanm on Sunday March 24, 2013 @09:11PM (#5434708)
You're right - that is the normal way (although not commonly seen). I'll update if I find it.