Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It's fairly straightforward if you're a C programmer and think of arrays as contiguous blocks of memory.

int * a = (int * )malloc(5);

a is a pointer to the beginning of an integer (default 4 byte) array. a[i] == a + i == ((uint8_t * )a)+i*sizeof(int). Therefore, a[0] == a + 0 == the first element in a contiguous block of memory.




which would also illustrate what K&R said about array indices just being syntactic sugar for pointer arithmetic.

My mind has been blown.


If that blows your mind, you might want to avoid looking at the following: "foo"[2] == ("foo"+2) == (2+"foo") == 2["foo"]. And yes, you really can do that: in general a[b] and b[a] are exactly the same thing in C.


More C array pointer fun:

  *("abcdefg" + 3) == 'd'


sure as long as you don't care about stuff like unicode..


not quite. "foo"[2] is a char, ("foo"+2) is a string.


oh, bother, I forgot what HN does to asterisks. In what I wrote above, look for the bit in italics and mentally insert asterisks on each side of it. It was right when I typed it in, I promise...




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

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

Search: