Live data from Hacker News

C array types are weird

anselmschueler.com

11–20 of 148 posts

Re: C array types are weird

#11
post #10
post #5

Earlier quoted context omitted.

What is **int[3][5]

A syntax error. You need a variable name, not a type name, in the middle.

And if you want 'int **arr[a][b]', it's a value that when you say 'x = **arr[m][n]', will evaluate to an int and assign it to x. Postfix has higher precedence than prefix.

Re: C array types are weird

#15
post #2

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

I know of this notations but I don't see many people using [static n].

Not sure why, maybe it doesn't feel like C anymore, maybe it feels hacky?

typically if you're passed an array you'd want to get more anyway, so you'd get passed a struct. Not sure.

Re: C array types are weird

#16
post #9

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…

This talk – "Programming without pointers" – by Andrew Kelley may be interesting to you. https://www.hytradboi.com/2025/05c72e39-c07e-41bc-ac40-85e83...

Learning to program with pointers is enormously useful. It's simply bad software engineering to not use typing to enforce constraints on access to pointers (or addresses, or however you'd like to term them)

Re: C array types are weird

#19
post #5
post #2

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

What is **int[3][5]

A pointer to a pointer to a pointer to a pointer of integers.

Re: C array types are weird

#20

Earlier quoted context omitted.

Yes it does. It just decays to a pointer at the slightest touch.

[flagged]

Because doing a dance to avoid it decaying conveys better information to both the compiler and downstream users of your code.
Post reply on HN