4 ms·
My favorite C "quirk": If you have an array and you want to access an item of it, you can swap the variable and the index number (put the variable name inside b
by fb03 4y ago
My favorite C "quirk": If you have an array and you want to access an item of it, you can swap the variable and the index number (put the variable name inside brackets and the number outside):
a[5]
is the same as:
5[a]
why?
a[5] is actually sugar for *(a + 5), so by commutative property, you can also do *(5 + a) to access the same memory position :-)
- jwilk 4y agoThat's #15 on the list.
- brookst 4y ago*15#
- notpushkin 4y agoConnection problem or invalid MMI code.
- yccs27 4y agoThat‘s #list on the 15.
- cptnapalm 4y ago&(list + 15) = https://gist.github.com/fay59/5ccbe684e6e56a7df8815c3486568f01#15-indexing-into-an-integer-httpsgodboltorgziba5gr https://gist.github.com/fay59/5ccbe684e6e56a7df8815c3486568f...
- the_svd_doctor 4y ago*(list + 14) ?
- FartyMcFarter 4y agoOne funny variant is this expression: "abcde"[4]
- whoopdedo 4y agoYou mean: 4["abcde"]
- FartyMcFarter 4y agoActually yes, oops :)
- deleted 4y ago[deleted]
- int_19h 4y agoIt's one of the B leftovers in C - In B, the only type is "machine word", and words are interpreted as ints or pointers depending on the operators used. Thus, distinguishing between a[i] and i[a] is impossible, so both were valid. Array-to-pointer decay is another manifestation of this.
- vram22 4y agoWas about to post this. I may have posted it before on HN. IIRC, I first read about it in the K&R C book.