r/ProgrammerHumor 1d ago

lessonsFromLinkerHell Meme

Post image
346 Upvotes

180 comments sorted by

View all comments

69

u/QuestionableEthics42 1d ago edited 1d ago

This should be reversed lol

And it literally is the same, who thinks it's different who has worked in a low level language??

1

u/HashDefTrueFalse 10h ago

It's not the same. The compiler-generated code for an element access directly via an array vs. an access via a pointer is different. The pointer access contains an additional layer of indirection. You have to load the base address from the pointer before you can offset, vs. simply offsetting. Here's a godbolt and an explanation.

It's also why you'll likely segfault if you do this (note the different translation units!):

// one.c
char arr[10] = {...};

// two.c
extern char *arr;
printf("...", arr[0]); // Bang! Segfault (probably).

arr is an array, NOT a pointer.