Declaration and Initialization

Array Declaration and Initialization in C Explained

Learn array declaration and initialization in C: set sizes, fill elements with braces, use {0} to zero an array, and avoid common size mistakes.

On this page
  1. Short answer
  2. Declaring an array
  3. Brace lists fill the elements
  4. Arrays cannot be assigned as a whole
  5. Character arrays
  6. Counting the elements
  7. Multidimensional arrays
  8. Arrays of structs
  9. Setting chosen elements
  10. Common mistakes
  11. Summary

Short answer

An array declaration gives the array a name, an element type, and a size:

int scores[5];

Initialization sets starting values for the elements, usually with a brace list:

int scores[5] = {90, 85, 77, 60, 99};

Elements are numbered from 0, so this array has indexes 0 through 4.

Declaring an array

int scores[5];      /* five int elements */
char initial[3];    /* three char elements */

If the array is a local variable with no initializer, its elements hold indeterminate values. Reading them before they are set is a bug. An array at file scope, or one declared static, starts with every element set to zero.

The size must be a constant known when the program is compiled. Use a number or a #define macro:

#define SIZE 5
int scores[SIZE];

In C, a const variable does not count as a constant here, so const int n = 5; int a[n]; is a variable-length array. C++ accepts const values as sizes, but C does not.

Brace lists fill the elements

int scores[5] = {90, 85, 77, 60, 99};

The values go in order, so scores[0] is 90 and scores[4] is 99.

If you leave out the size, the compiler counts the list:

int primes[] = {2, 3, 5, 7, 11};   /* the size is 5 */

If the list is shorter than the array, the remaining elements are set to zero:

int partial[5] = {1, 2};           /* partial holds {1, 2, 0, 0, 0} */

To set every element to zero, use a single 0 in the braces:

int zeros[100] = {0};              /* every element is 0 */

A list with too many values is an error:

int small[2] = {1, 2, 3};          /* too many initializers */

C++ rejects this with an error. Many C compilers report it as a warning.

Arrays cannot be assigned as a whole

After the declaration, you cannot copy one array into another with =, and you cannot assign a brace list either:

int a[3] = {1, 2, 3};
int b[3];

b = a;                  /* error: array types cannot be assigned */
b = {4, 5, 6};          /* error: a brace list is only an initializer */

To copy the values, loop over the elements, or use memcpy from <string.h> in C. If you are writing C++, c std::array gives you a fixed-size array that can be copied with =.

Character arrays

char name[] = "Ann";   /* size 4: 'A', 'n', 'n', and the terminating '\0' */

When you size a character array by hand, leave room for the terminating '\0'. A string without that terminator is not safe to use as a C string.

Counting the elements

You can count the elements of an array in the same scope where it is declared:

int primes[] = {2, 3, 5, 7, 11};
int count = sizeof(primes) / sizeof(primes[0]);   /* 5 */

This works only where primes is a real array. If you pass the array to a function, the function receives a pointer, and sizeof then gives the size of the pointer. The details are explained in how to pass an array to a function in c.

Multidimensional arrays

Each inner brace list fills one row:

int grid[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

After this, grid[1][2] is 6.

Arrays of structs

The brace rules carry over to an array of structs, where each inner brace list fills one struct:

struct point {
    int x;
    int y;
};

struct point path[2] = { {0, 0}, {3, 4} };

If you want a fixed set of structs in an array, this is the usual setup. The same pattern appears in array of structs in c.

Setting chosen elements

C designated initializers, added in C99, can set chosen elements by index:

int sparse[10] = { [7] = 1 };      /* only sparse[7] is set; the rest are 0 */

Common mistakes

  • Off-by-one indexes. A 5-element array has indexes 0 to 4. Using scores[5] reads or writes past the end. The compiler often does not catch this, and the program has undefined behavior.
  • A missing size. Inside a function, int values[]; with no size and no initializer is an error, because the compiler cannot tell how big the array is.
  • A runtime size. int n = 5; int a[n]; is not allowed in C++. In C, it creates a variable-length array, which is better avoided while you are learning.
  • Assuming assignment works. Arrays are set in the declaration. Afterward, copy them with a loop or memcpy.

Summary

  • A declaration sets the name, element type, and size.
  • A brace list fills the elements in order. Missing elements become zero, and {0} zeros the whole array.
  • Arrays cannot be assigned as a whole. Copy them with a loop or memcpy in C.
  • Use sizeof to count elements in the scope where the array is declared.

The complete guide to declaration and initialization

Designated Initializers in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

Header Guards in C++

A header guard stops a header from being included twice in one source file. Learn how #ifndef and #pragma once work, with examples and common mistakes.

Header Guards 4 min read