Consider `double A[5] = { [0] = 9.0, [1] = 2.9, [4] = 3.E+25, [3] = .00007 };`. An expression such as `A[4]` selects an element; here it has the value `3.E+25`. Indices measure positions from the start, so a five-element array has indices 0 through 4. The bracketed numbers inside the initializer have a different job: they designate elements to initialize. The omitted element, `A[2]`, is initialized to zero. section 2.3 section 2.2
The same notation connects array access to pointer operations:
- `A[i]` and `*(A+i)` are equivalent whether `A` is an array or a pointer. When an array is evaluated where a value is needed, it decays to a pointer to its first element. Indexing a pointer does not create an array object or supply its length. section 11.3.1 section 11.1.2
- In a function declaration, an array parameter is rewritten as a pointer parameter. Put length parameters before array parameters when a declaration uses those lengths; a pointer alone cannot reveal the array’s length. section 11.3.2 section 11.1.2
- Keep access within the relevant array object. Pointer arithmetic can itself be invalid when it exceeds array bounds; an expression such as `a+len` can mark the position just beyond an array, but it is not an element to read. Prefer `A[i]` over `*(A+i)` when expressing an element access: compilers may more readily detect an invalid index. section 15.1.1 section 11.1.2
void matrix_mult(size_t n, size_t k, size_t m,
double C[n][m],
double const A[n][k],
double const 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];
}
}
}
}
In matrix multiplication, `C[i][j]`, `A[i][l]`, and `B[l][j]` select elements by two indices. The declared dimensions and loop limits show how each access is intended to stay within its matrix. section 11.3.2
TAKEAWAY: Use indexing to name the element you intend to access, but do not mistake readable syntax for a bounds check. When passing an array to a function, pass the length information the function needs. section 15.1.1 section 11.1.2














