Hacker News new | past | comments | ask | show | jobs | submit login

>Older C compilers did let you get away with more, like using integers as pointers

In older C compilers (maybe pre-ANSI, or later, can't remember), you could literally write i[a] instead of a[i], where a was an array and i was an int, and it would work equivalently, i.e. no compiler error, and give the same result as a[i]. This was because a was the address of the start of the array, and i was an offset, so a[i] actually meant *(a + i), which, by the commutative property of arithmetic, was equivalent to *(i + a), which was equivalent to i[a].

I had read this in some C book, maybe K&R or the Waite Group C Microsoft Bible.

Never forgot it, because it was counter-intuitive, unless you knew the above reason.

And tried it out in one or more C compilers at that approximate period, and it worked as stated.




This is still true, and it's mandated by the ISO C standard that a[i] is equivalent to (*((a)+(i))) (yes, they specify it with that many parentheses). You're still able to compile code that treats a[i] and i[a] interchangeably.

https://godbolt.org/z/37Kv44o1b

> I had read this in some C book, maybe K&R or the Waite Group C Microsoft Bible.

I haven't read the Microsoft C Bible, but it does say this in K&R while explaining pointers.


>This is still true, and it's mandated by the ISO C standard ...

Wow, good to know.

>but it does say this in K&R

Then that must be where I had read it.


This still works iirc.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: