logoalt Hacker News

C array types are weird

58 pointsby signa11last Monday at 5:05 AM29 commentsview on HN

Comments

fookertoday at 1:04 AM

C array types are weird because C doesn't really need arrays. It's not what C was about.

But if you designed a language in the era where Fortran, THE array language, reigned supreme, nobody would use your language. The mindshare Fortran had is difficult to convey now, half a century later.

Think of it like making a chatbot today and not mentioning AI or LLMs, that's what making a language without arrays would have felt like in 1970.

ueckerlast Monday at 5:49 AM

In practice, the [static n] notation can give you useful warnings and bounds checking.

https://godbolt.org/z/PzcjW4zKK

And while the (*array_ptr)[3] notation take a moment to get used to, it is very logical. If you have a pointer to an array, you dereference it first and then indx into it. Again, useful for bounds checking: https://godbolt.org/z/ao1so9KP7

show 2 replies
kazinatortoday at 12:21 AM

There is a history to it; in one of the predecessor languages, like B, Ritchie actually had arrays that had a hidden pointer to their start. The "array to pointer decay" was actually a real operation that loaded an address from memory, and it was possible to twiddle the bits to relocate an array. One problem with it was no way to initialize such a pointer field that would allow an array to live in dynamically allocated storage (no constructors in the language).

So in short, the bad design (array values produce pointers) was informed by conceptual compability with an earlier design in which that was literally happening.

the__alchemistyesterday at 10:32 PM

This is one of the things that I feel is an inappropriate abstraction that is around for historical reasons. When I do FFI to call C from rust, I usually wrap the generated API (Which is pointer based) into rust's &[] array syntax. Arrays/lists/Vecs etc in most non-C languages feel like an abstraction over a collection of items; I feel like C's exposing the pointer directly is taking a low-level memory/MMIO operation and inserting it into business logic. Conceptually, I like to keep them separate; pointers for writing drivers, accessing registers, writing to flash memory etc. Arrays/lists/vecs for higher level operations on collections.

Tangent: I have a pet theory that part of Zig's raison d'etre is to fix some of the problems with C, while accommodating its pointer-based data structures, and the resulting patterns.

show 1 reply
mlmonkeytoday at 1:23 AM

It still cracks me up that 3[x] and x[3] mean the same thing in C.

IncreasePostsyesterday at 10:19 PM

Paging walter bright

show 2 replies
fatty_patty89yesterday at 10:17 PM

there's no array type in c

show 1 reply
throwaway27448yesterday at 11:19 PM

Why are we still discussing c in 2026? Why are you intentionally hamstringing yourself unless you're using fucking hp-ux

show 2 replies