int array[] = {1,2,3};

  • 6
    @iamavalos But that doesn't compile, right? Mine does. It absolutely horrible, cursed code, but it compiles (and returns 1).
  • 5
    This is just fucked up horrible piece of shit code. My eyes hurt.

    9 years of experience in C and i still TBH dont know how exactly does this work as i never used something like that. Like WTF
  • 4
    After compiling i understood and i just want to punch my monitor for even displaying that.
  • 3
    @Fabian i feel bad now but i cant find it anywhere. How the fuck is it even possible that this: 0[array] even fucking compiles and works ? Like WTF ?
  • 9

    It's all syntactic sugar.

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

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

    Addition is commutative, so you can swap the operands.
  • 1
    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
  • 3
    @taigrr thanks for explaining.
  • 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.
  • 2
    @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 :=(
Your Job Suck?
Get a Better Job
Add Comment