C array types are weird
2 days ago
- #arrays
- #language design
- #C programming
- Array types in C (T[n]) are distinct from pointer types (T*), but array expressions decay to pointers to the first element.
- The sizeof operator works differently on arrays versus pointers, returning the total byte size for arrays but pointer size for decayed pointers.
- Function signatures treat array parameters as pointers, losing size information, unlike when using pointers to arrays.
- A hypothetical improved C could treat arrays like structs, passing by value to avoid confusion, with copying implications.
- The @ operator from GDB is suggested as a syntax for constructing arrays from pointers, allowing explicit length annotation.
- Array slicing could be made natural with syntax like iota[1]@2, addressing length metadata issues in current C.
- The -> operator's design is discussed, with an alternative syntax proposed to emphasize pointer-level operations.