Skip to content
Tutorials Center
My learning

Topics

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.

C
int scores[5] = {90, 85, 77};   /* remaining elements are 0 */
size_t n = sizeof scores / sizeof scores[0];

Try it

C Declaring, initializing and measuring

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.

C Decay: sizeof inside a function

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

C
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 separate size_t.
  • Decay: a becomes &a[0] except as an operand of sizeof, unary &, alignof, or a string literal initializing a char array.
  • No array assignment or comparison. b = a and a == b do not copy or compare contents. Use a loop, memcpy, or memcmp. Wrap an array in a struct to copy it by assignment.
  • Parameters int arr[] and int arr[10] both mean int *arr. The 10 is ignored, except in the C99 form int arr[static 10], which promises at least 10 elements.
  • Multidimensional arrays are row-major: grid[i][j] sits at offset i * cols + j. Only the first dimension may be omitted in a parameter.
  • Uninitialized local arrays contain indeterminate values; global and static arrays start at zero.
  • Large local arrays live on the stack and can overflow it; use static or malloc for megabytes.
  • C23: an empty initializer = {} is allowed and zeroes the array.