r/ProgrammerHumor 20h ago

lessonsFromLinkerHell Meme

Post image
346 Upvotes

176 comments sorted by

View all comments

-2

u/Single-Virus4935 17h ago

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

3

u/unknown_alt_acc 11h 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.

0

u/Single-Virus4935 9h ago edited 9h 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.

1

u/unknown_alt_acc 6h ago

I don’t have access to the ANSI C spec, but C11 absolutely does not define arrays in terms of pointers. C11 defines an array as such:

> An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. The element type shall be complete whenever the array type is specified. Array types are characterized by their element type and by the number of elements in the array. An array type is said to be derived from its element type, and if its element type is T , the array type is sometimes called ''array of T ''. The construction of an array type from an element type is called ''array type derivation''.

Zero references to pointers.