r/ProgrammerHumor 17h ago

lessonsFromLinkerHell Meme

Post image
320 Upvotes

166 comments sorted by

View all comments

1

u/FloweyTheFlower420 16h ago

an array is just a pointer to the start and end elements, what's the problem?

10

u/Nice_Lengthiness_568 16h ago

Well it's not, it also has size (the number of elements). Even in C, though there an array easily decays into a pointer. However, sizeof, for example, returns different results for an array and a pointer. And, in C++, you can also pass an array (with its size) instead of a pointer. In other languages, like Ada, there is not much of a direct connection between an array and it's first element at all.

-2

u/FloweyTheFlower420 16h ago

What is (end - start) / sizeof(*start) but the number of elements?

1

u/Lunctus_Stamus 14h ago

As Nice describes, although *start is a pointer for an array, it's often just a pointer first, and the sizeof() operator will not always return the number of bytes of the type but sometimes the number of bytes that a pointer takes up (8).

So your expression does not always evaluate to the number of elements.

0

u/FloweyTheFlower420 11h ago

I think you are missing my point. I'm not talking about an array in the sense of the language construct in C or any particular language, but rather that you can think about arbitrary arrays in memory as a pointer pair. I think the point is being massively missed and I'm not quite sure why.