I have been thinking of a couple of possible extensions to the C language which I'd like to know the opinion of others about.1 This one is about multi-dimensional arrays.
Imagine the following situation in C:
void matrix_mul(double **res, double **a, double **b, size_t n, size_t m, size_t r);
You cannot pass a double mat[4][4]
(for example) to this function. If you define the function as:
void matrix_mul_N_M_R(double res[N][R], double a[N][M], double b[M][R]);
You'd just be wasting a perfectly written fine algorithm on a certain size of matrices.
What came to me (in shower obviously), was decay of multi-dimensional arrays to multi-level pointers. This is an extension to decay of 1d arrays to pointers.
Semantically, this would allow the first type of function to work on any two-dimensional array. Technically, this is how it would be implemented. The following code:
double a[3][5];
double b[5][4];
double c[3][4];
...
matrix_mul(c, a, b, 3, 5, 4);
would be equivalent to:
double a[3][5];
double b[5][4];
double c[3][4];
...
double **__a_decay = {a[0], a[1], a[2]}; /* generated by compiler */
double **__b_decay = {b[0], b[1], b[2], b[3], b[4]}; /* generated by compiler */
double **__c_decay = {c[0], c[1], c[2]}; /* generated by compiler */
matrix_mul(__c_decay, __a_decay, __b_decay, 3, 5, 4); /* first three arguments decayed */
Is the application for such an extension very limited? Are there pitfalls? Could this possibly lead to insecure code where not using this feature wouldn't? Where else could such a feature come in handy?
1 Note that I'm not going to make another language myself, but would like to be sure of the feature's benefits and pitfalls before trying to suggest them to the C Standard Committee.