Arrays
An array is a contiguous sequence of objects of one type, addressed by an integer index starting at zero. In most expressions the array name decays to a pointer to the first element.
Detailed Explanation
int scores[4]; reserves space for four ints back to back. scores[0] is the first, scores[3] is the last. There is no bounds check. scores[4] is undefined behavior, and so is scores[-1]. The compiler will not stop you. Tools such as AddressSanitizer and Valgrind will, after the fact.
The length is part of the type when it is a constant: int scores[4] and int scores[5] are different types. A function cannot receive an array by value. The parameter void f(int a[4]) is rewritten by the compiler to void f(int *a). The 4 is a comment the type checker ignores. Pass the length as a separate size_t.
Decay is the rule that surprises people coming from other languages. In an expression, scores becomes &scores[0], a pointer to the first int. The exceptions are sizeof scores, &scores, and _Alignof scores. sizeof scores is the size of the whole array in bytes. sizeof &scores[0] is the size of a pointer. That is why sizeof a / sizeof a[0] works in the scope where a is still an array and produces 1 — or a nonsense value — inside a function that received a as a parameter.
Variable-length arrays (VLAs), added in C99 and made optional in C11, let the length come from a run-time value: int buf[n];. The storage is automatic, so a large n blows the stack. Many style guides ban VLAs for that reason and because some compilers (notably MSVC) never implemented them. Prefer malloc when the size is not a small compile-time constant.
Static arrays at file scope live for the whole program and are zeroed if you do not initialize them. A compound literal (int[]){1, 2, 3} creates an unnamed array; at block scope it is automatic. String literals are arrays of char with a static duration and a hidden '\0'.
Code Examples
#include <stdio.h>
static void print_ints(const int *a, size_t n) /* length is a real parameter */
{
for (size_t i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
int main(void)
{
int scores[] = { 9, 6, 10, 7 }; /* length inferred: 4 */
size_t n = sizeof scores / sizeof scores[0];
print_ints(scores, n); /* prints: 9 6 10 7 */
printf("%zu %zu\n", sizeof scores, sizeof &scores[0]);
/* typical 64-bit: 16 8 — array size versus pointer size */
return 0;
}#include <stdio.h>
#include <stdlib.h>
int main(void)
{
size_t n = 4;
int *heap = malloc(n * sizeof *heap);
if (heap == NULL) {
return 1;
}
for (size_t i = 0; i < n; i++) {
heap[i] = (int)i * (int)i;
}
printf("%d\n", heap[3]); /* prints: 9 */
free(heap);
return 0;
}Common Gotchas
Index `n` of an `n`-element array is out of bounds
valid indices are `0` through `n - 1`. Off-by-one here is a buffer overflow.
`sizeof a` inside a function is the size of a pointer if `a` is a parameter
compute the length at the call site and pass it in.
VLAs live on the stack
`int buf[n];` with `n` from input is a stack-overflow gadget. Use `malloc` for untrusted sizes.