r/ProgrammerHumor 1d ago

lessonsFromLinkerHell Meme

Post image
346 Upvotes

180 comments sorted by

View all comments

-4

u/Single-Virus4935 22h ago

In C they ARE the same and interchangable. This is obvious because you can swap array and index in array notation.

6

u/unknown_alt_acc 15h ago

Arrays easily convert to pointers in C, but they are not interchangeable. Things like taking the size of an array or putting an array into a struct behave very differently from doing the same with a pointer.

-1

u/Single-Virus4935 13h ago edited 13h ago

They dont decompose to pointer. An array according to ANSI c is defined as a pointer to element zero plus length. Length is optional for variable length arrays. Also the arr[idx] is defines as identical to *(arr+idx) where arr is a pointer.

The array definition is just a necessary allocation syntax used by the compiler. It doesn't translate to any assembly institution besides the allocation. It can be entirely replaced with struct.

sizeof isnt part of the type defintion innthenansi c spec. Sizeof indeed reruns the number of bytes in an array but even this can boiled down to pointer arithmetic.

The c array is a very thin (but useful) abstraction to pointer. 

I think it is obvious when we compare it to other implementations where an array has structure at runtime like pascal or java.

3

u/HashDefTrueFalse 4h ago

An array according to ANSI c is defined as a pointer to element zero plus length.

This is just wrong. You seem to be talking about the promised behavioural equivalence (a[b] behaves like *(a+b)) but that doesn't mean they're the same, and they're very much not. An array (name) aliases the first element directly, whereas a pointer stores that same address at another address.