C Reference ยท Types and Literals
Arrays in C
Reference for C arrays: declaring and initializing, sizeof for element counts, array-to-pointer decay, multidimensional arrays and why you can't assign arrays.
An array is a fixed number of objects of the same type stored one after another in memory. You declare one with square brackets, int scores[5];, and you reach element i with scores[i], counting from zero. The length is part of the type: int[5] and int[6] are different types.
Arrays are not first-class values in C. You cannot assign one array to another, and in most expressions an array name turns into a pointer to its first element. That conversion, called decay, is the source of most array surprises.
int scores[5] = {90, 85, 77}; /* remaining elements are 0 */ size_t n = sizeof scores / sizeof scores[0];
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
type name[length]; /* length: constant expression */ type name[length] = { v0, v1 }; /* unlisted elements are zero */ type name[] = { v0, v1, v2 }; /* length from initializer */ type name[rows][cols]; /* array of arrays */
| Part | Description |
|---|---|
type |
Element type; any complete object type (not void, not a function). |
length |
A constant expression greater than zero. A non-constant length makes a variable length array. |
{ ... } |
Initializer list. Fewer values than elements zero the rest; more is a constraint error. |
Returns: a[i] is the element, equivalent to *(a + i).
Errors / undefined behavior: accessing an index outside 0 to length - 1 is undefined behavior; there is no bounds check. Forming a pointer more than one past the end is also undefined.
Usage notes
- Count elements with
sizeof a / sizeof a[0]while the name still is an array. Once passed to a function it is a pointer and this trick gives the wrong number; pass the length as a separatesize_t. - Decay:
abecomes&a[0]except as an operand ofsizeof, unary&,alignof, or a string literal initializing achararray. - No array assignment or comparison.
b = aanda == bdo not copy or compare contents. Use a loop,memcpy, ormemcmp. Wrap an array in astructto copy it by assignment. - Parameters
int arr[]andint arr[10]both meanint *arr. The 10 is ignored, except in the C99 formint arr[static 10], which promises at least 10 elements. - Multidimensional arrays are row-major:
grid[i][j]sits at offseti * cols + j. Only the first dimension may be omitted in a parameter. - Uninitialized local arrays contain indeterminate values; global and
staticarrays start at zero. - Large local arrays live on the stack and can overflow it; use
staticormallocfor megabytes. - C23: an empty initializer
= {}is allowed and zeroes the array.