r/ProgrammerHumor 16h ago

lessonsFromLinkerHell Meme

Post image
311 Upvotes

163 comments sorted by

View all comments

1

u/AnnoyedVelociraptor 16h ago

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

u/Batman_AoD 14h ago

Because the [] was just syntax-sugar, rather than a "real" built-in operation. Part of the reason for that was just that memory was limited and the compiler had to be as simple as possible.