Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Fabian11306y@iamavalos But that doesn't compile, right? Mine does. It absolutely horrible, cursed code, but it compiles (and returns 1).
-
taigrr8796y@Haxk20
It's all syntactic sugar.
arr[index] == *(arr + index)
index[arr] == *(index + arr)
Addition is commutative, so you can swap the operands. -
taigrr8796yIf 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
-
Fabian11306yI 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.
-
For all those with puzzled looks: in C, array[index] and index[array] both translate to *(array+index)
Related Rants
int array[] = {1,2,3};
printf("%i",0[array]);
joke/meme
c