12
Fabian
5y

int array[] = {1,2,3};
printf("%i",0[array]);

Comments
  • 5
    @iamavalos But that doesn't compile, right? Mine does. It absolutely horrible, cursed code, but it compiles (and returns 1).
  • 9
    @Haxk20

    It's all syntactic sugar.

    arr[index] == *(arr + index)

    index[arr] == *(index + arr)

    Addition is commutative, so you can swap the operands.
  • 3
    If you think about it, in my example arr is just a memory address, and you're offsetting it by an index. Which is why int x = arr[0] is the same as int x = *arr
  • 1
    I like how the second line attracted so much attention, but the first didn't at all. I've heard that there are actually some crazy people out there you seriously write "int array[]" in real code, but they should get punched for that.
  • 1
    @Haxk20 If you've ever wondered why arrays start at zero, this is why.
  • 0
    For all those with puzzled looks: in C, array[index] and index[array] both translate to *(array+index)
  • 1
    Ups, I hadn't seen @taigrr 's reply and essentially repeated the same :=(
Add Comment