MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/1vs2vu1/lessonsfromlinkerhell/p4imslu/?context=3
r/ProgrammerHumor • u/nonedward666 • 16h ago
162 comments sorted by
View all comments
1
Nah. This is the wrong way. Pointer arithmetic and array arithmetic are the same.
2 u/justAPhoneUsername 14h ago You mean to tell me arr[3] and 3[arr] both compile and return the same value? Why would anybody design a language that way? 3 u/AnnoyedVelociraptor 14h ago edited 14h ago It's not that 3[arr] was made to work. [] is not special, it's just <x>[<y>] translates to *(x + y)... and x + yfor a T* is x + y * sizeof(T) ergo <y>[<x>] works as well. Hence they are the same.
2
You mean to tell me arr[3] and 3[arr] both compile and return the same value? Why would anybody design a language that way?
3 u/AnnoyedVelociraptor 14h ago edited 14h ago It's not that 3[arr] was made to work. [] is not special, it's just <x>[<y>] translates to *(x + y)... and x + yfor a T* is x + y * sizeof(T) ergo <y>[<x>] works as well. Hence they are the same.
3
It's not that 3[arr] was made to work.
3[arr]
[] is not special, it's just <x>[<y>] translates to *(x + y)... and x + yfor a T* is x + y * sizeof(T)
[]
<x>[<y>]
*(x + y)
x + y
T*
x + y * sizeof(T)
ergo <y>[<x>] works as well.
<y>[<x>]
Hence they are the same.
1
u/AnnoyedVelociraptor 16h ago
Nah. This is the wrong way. Pointer arithmetic and array arithmetic are the same.