How To Declare An Array C++

4 min read

Declaring an array in C++ means creating a fixed-size collection of variables that share the same data type and are stored in continuous memory. Still, if you need to store a list of student grades, temperatures, scores, or coordinates, an array is one of the most basic and efficient tools in C++. Learning how to declare an array in C++ correctly helps you write cleaner code, avoid memory mistakes, and understand more advanced structures such as std::array, std::vector, matrices, and multidimensional data Easy to understand, harder to ignore..

What Is an Array in C++?

An array is a collection of elements of the same type. Instead of declaring many separate variables like score1, score2, and score3, you can declare one array and access each element using an index.

For example:

int scores[5] = {90, 85, 78, 92, 88};

This declares an array named scores that can hold 5 integers.

Each element in the array can be accessed by its index:

scores[0] // 90
scores[1] // 85
scores[2] // 78

C++ array indexes start at 0, so an array with 5 elements has valid indexes from 0 to 4 It's one of those things that adds up. That's the whole idea..

Basic Array Declaration Syntax

The basic syntax for declaring a built-in C++ array is:

type arrayName[arraySize];

For example:

double prices[3];

This declares an array named prices that can store 3 double values The details matter here..

The type can be any valid C++ data type, such as:

  • int
  • double
  • float
  • char
  • bool
  • A custom class or struct type

Example:

int numbers[4];
double amounts[4];
char letters[4];
bool flags[4];

The number inside the square brackets is the array size, meaning how many elements the array can hold.

Declaring and Initializing an Array

You can declare an array and assign values at the same time using an initializer list.

int ages[3] = {25, 30, 35};

This creates an array with three elements:

ages[0] = 25;
ages[1] = 30;
ages[2] = 35;

You can also let C++ determine the size automatically when you provide initial values:

int values[] = {10, 20, 30, 40};

Here, the compiler counts the four values and creates an array of size 4.

Another common way to initialize an array is to set all elements to zero:

int numbers[5] = {};

After this declaration, all five elements are initialized to 0.

Array Size Must Usually Be Known at Compile Time

For standard C++, the size of a built-in array must be a constant expression. This means the compiler needs to know the size before the program runs.

At its core, valid:

int arr[10];

This is also valid:

const int size = 10;
int arr[size];

That said, this is not standard C++:

int n;
std::cin >> n;
int arr[n];

Some compilers allow this as an extension, but it is not portable and should not be used in standard C++ code. If the size is known only at runtime, use std::vector instead.

Accessing Array Elements

Once an array is declared, you can access or modify elements using square brackets:

int scores[5] = {90, 85, 78, 92, 88};

scores[0] = 95;
std::cout << scores[2] << std::endl;

The expression inside the square brackets is called the index. C++ uses zero-based indexing, so the first element is at index 0, and the last element is at index arraySize - 1.

For an array with 5 elements:

scores[0]
scores[1]
scores[2]
scores[3]
scores[4]

This is valid:

scores[4] = 100;

This is invalid:

scores[5] = 100;

Accessing an element outside the array bounds causes undefined behavior. The program may crash, print incorrect values, or appear to work by mistake.

Declaring an Array Without Initializing It

You can declare an array without giving it initial values:

int numbers[5];

This creates an array of 5 integers, but the elements are not automatically set to zero when the array is a local variable.

int main() {
    int numbers[5];

    std::cout << numbers[0]; // Undefined behavior if read before initialization
}

If you want all elements initialized to zero, use:

int numbers[5] = {};

For global or static arrays, elements are automatically initialized to zero, but relying on this behavior can make code harder to understand. It is usually clearer to initialize arrays explicitly And it works..

Arrays of Different Data Types

Arrays can store any data type Easy to understand, harder to ignore..

Integer Array

int scores[4] = {88, 91, 76
This Week's New Stuff

Recently Shared

Others Liked

You Might Find These Interesting

Thank you for reading about How To Declare An Array C++. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home