An array is a run of elements of one type, laid out contiguously - element i sits exactly i * sizeof(element) bytes after the first. That single fact is why indexing is fast, why pointer arithmetic works, and why there is no bounds checking: a[i] is defined as *(a + i), which is arithmetic, and arithmetic does not know where your array ends.
Reading or writing outside an array is undefined behaviour, not an error. It may crash, may silently corrupt the variable next to it, or may appear to work until the data changes. This is the single largest source of security vulnerabilities in C programs.
A multi-dimensional array is stored in row-major order: m[2][3] is three ints, then three more. So iterating rows in the outer loop and columns in the inner walks memory in order and uses the cache well, while the reverse jumps around - on a large matrix the same arithmetic runs several times slower purely because of the access pattern.
The thing to understand before pointers: an array passed to a function decays to a pointer to its first element. The function receives an address, not an array, so sizeof inside the function gives the size of a pointer rather than the size of the array. The length does not travel with it, which is why every C function that takes an array also takes its length.
#include <stdio.h>
/* The parameter is a POINTER. int arr[] is the same declaration as
int *arr here - the brackets are decoration. So the length has to
be handed over separately; it does not travel with the array. */
int sum(const int arr[], size_t n)
{
printf(" inside: sizeof(arr) = %zu\n", sizeof arr); /* 8: a pointer */
int total = 0;
for (size_t i = 0; i < n; i++) total += arr[i];
return total;
}
int main(void)
{
int values[5] = {3, 1, 4, 1, 5};
printf("outside: sizeof(values) = %zu\n", sizeof values); /* 20 */
size_t n = sizeof values / sizeof values[0]; /* only valid HERE */
printf("sum = %d\n", sum(values, n));
/* Contiguous: each element is sizeof(int) after the last. */
for (size_t i = 0; i < n; i++)
printf("%zu at %p\n", i, (void *) &values[i]);
/* Row-major: m[0] is three ints, then m[1] is the next three. */
int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
/* Rows outer, columns inner - walks memory in order. The reverse
jumps a row-width each step and is far slower on a big matrix. */
for (size_t r = 0; r < 2; r++) {
for (size_t c = 0; c < 3; c++) printf("%d ", m[r][c]);
printf("\n");
}
/* a[i] is DEFINED as *(a + i). There is no bounds check, because
there is nothing to check against - it is arithmetic. */
/* printf("%d\n", values[99]); undefined behaviour, not an error */
return 0;
}