3 ms·
I would go all-in and make arrays a distinct type, so the only way to create an array is with the [..] syntax, and forbid implicit type compatibility between ar
by tadfisher 4y ago
I would go all-in and make arrays a distinct type, so the only way to create an array is with the [..] syntax, and forbid implicit type compatibility between arrays and pointers (which would discard the dimension part of the array type). An explicit conversion (cast) from pointer-to-array should require the dimension as part of the type.
So &c on a char should produce char, and &c on a char[20] should produce char[20].
- tylerhou 4y agoWhy not standardize something like std::span in C?
- pjmlp 4y agoLack of willigness.
- _kst_ 4y agoArrays are already distinct types. char c; &c; // type is char* char arr[20]; &arr; // type is char (*p)[20], pointer to array of 20 char (A small quibble: the term "type compatibility" in C doesn't mean implicit convertibility. Two types are compatible if they're literally the same type, and in just a few other cases. You can assign an int value to a long object, but int and long are not compatible, even if they happen to be the same size.) The problem with dropping implicit array-to-pointer conversion is that it would break most existing C code. It would be a great idea for a new language. Suggested reading: Section 6 of the comp.lang.c FAQ, <https://www.c-faq.com/ https://www.c-faq.com/>. The relationship between arrays and pointers in C is admittedly confusing; this is the best resource I know of for explaining it.