Passing a length with an array
A summing function can receive the number of elements separately: `double sum0(size_t len, double const* a)`. Its loop uses `len` as the stopping condition while `a` supplies the elements. A call such as `sum0(3, &A[2])` sums three elements beginning at `A[2]`; the pointer itself does not reveal how many elements remain. section 11.1.2
An array length parameter conveys how many elements an array argument is expected to contain. In a function interface, array notation makes that relationship visible: `double sum(size_t len, double const a[len]);`. The length parameter must appear before the array parameter, because it must be known where the array bound is first used. section 11.3.2
Keep these rules distinct:
- A pointer cannot carry an array object's length. Passing an array through a pointer therefore requires passing its real length separately. section 11.1.2
- For an array object `A`, calculate its length as `(sizeof A) / (sizeof A[0])`; this works from the array object, not from a pointer. section 6.1.3 section 11.1.2
- In multidimensional array parameters, only the innermost dimension is rewritten as a pointer; the remaining dimensions retain array notation. section 11.3.2
- The programmer must ensure that every array argument actually satisfies the stated length; dynamic lengths receive little automatic protection. section 11.3.2
void matrix_mult(size_t n, size_t k, size_t m,
double C[n][m],
double A[n][k],
double B[k][m]) {
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < m; ++j) {
C[i][j] = 0.0;
for (size_t l = 0; l < k; ++l) {
C[i][j] += A[i][l]*B[l][j];
}
}
}
}
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":0,\"ch\":17},{\"line\":0,\"ch\":23}],[{\"line\":4,\"ch\":8},{\"line\":4,\"ch\":14}],[{\"line\":5,\"ch\":10},{\"line\":5,\"ch\":16}],[{\"line\":7,\"ch\":12},{\"line\":7,\"ch\":18}],[{\"line\":0,\"ch\":17},{\"line\":0,\"ch\":23}],[{\"line\":4,\"ch\":8},{\"line\":4,\"ch\":14}],[{\"line\":5,\"ch\":10},{\"line\":5,\"ch\":16}],[{\"line\":7,\"ch\":12},{\"line\":7,\"ch\":18}],[{\"line\":0,\"ch\":17},{\"line\":0,\"ch\":23}],[{\"line\":4,\"ch\":8},{\"line\":4,\"ch\":14}],[{\"line\":5,\"ch\":10},{\"line\":5,\"ch\":16}],[{\"line\":7,\"ch\":12},{\"line\":7,\"ch\":18}],[{\"line\":1,\"ch\":17},{\"line\":1,\"ch\":23}],[{\"line\":2,\"ch\":17},{\"line\":2,\"ch\":23}],[{\"line\":3,\"ch\":17},{\"line\":3,\"ch\":23}],[{\"line\":1,\"ch\":17},{\"line\":1,\"ch\":23}],[{\"line\":2,\"ch\":17},{\"line\":2,\"ch\":23}],[{\"line\":3,\"ch\":17},{\"line\":3,\"ch\":23}],[{\"line\":1,\"ch\":17},{\"line\":1,\"ch\":23}],[{\"line\":2,\"ch\":17},{\"line\":2,\"ch\":23}],[{\"line\":3,\"ch\":17},{\"line\":3,\"ch\":23}],[{\"line\":4,\"ch\":3},{\"line\":4,\"ch\":6}],[{\"line\":5,\"ch\":5},{\"line\":5,\"ch\":8}],[{\"line\":7,\"ch\":7},{\"line\":7,\"ch\":10}],[{\"line\":0,\"ch\":17},{\"line\":0,\"ch\":23}],[{\"line\":4,\"ch\":8},{\"line\":4,\"ch\":14}],[{\"line\":5,\"ch\":10},{\"line\":5,\"ch\":16}],[{\"line\":7,\"ch\":12},{\"line\":7,\"ch\":18}],[{\"line\":4,\"ch\":3},{\"line\":4,\"ch\":6}],[{\"line\":5,\"ch\":5},{\"line\":5,\"ch\":8}],[{\"line\":7,\"ch\":7},{\"line\":7,\"ch\":10}],[{\"line\":0,\"ch\":17},{\"line\":0,\"ch\":23}],[{\"line\":4,\"ch\":8},{\"line\":4,\"ch\":14}],[{\"line\":5,\"ch\":10},{\"line\":5,\"ch\":16}],[{\"line\":7,\"ch\":12},{\"line\":7,\"ch\":18}],[{\"line\":4,\"ch\":3},{\"line\":4,\"ch\":6}],[{\"line\":5,\"ch\":5},{\"line\":5,\"ch\":8}],[{\"line\":7,\"ch\":7},{\"line\":7,\"ch\":10}],[{\"line\":0,\"ch\":17},{\"line\":0,\"ch\":23}],[{\"line\":4,\"ch\":8},{\"line\":4,\"ch\":14}],[{\"line\":5,\"ch\":10},{\"line\":5,\"ch\":16}],[{\"line\":7,\"ch\":12},{\"line\":7,\"ch\":18}]]"}
!@%STYLE%@!
A matrix-multiplication implementation shows length parameters describing each matrix dimension and controlling indexed loops. section 11.3.2

