← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 1 of 15

C++ Arrays Introduction

An array is a row of boxes that hold the same kind of value, each with a number starting at 0. It is like a row of lockers you open by number.
Syntax
cpp
data_type array_name[size];

data_type array_name[size] = {value1, value2, value3};

array_name[index];  // access an element

What is an Array?

An array groups multiple values of the same data type under one variable name, stored back-to-back in contiguous memory, which is what makes accessing any element by its position both fast and predictable.

This contiguous layout is the key difference between an array and other collection types that might scatter their elements across memory.

Example: What is an Array?

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int scores[3] = {90, 85, 78};
	// Print to the console with cout
	std::cout << scores[0] << " " << scores[1] << " " << scores[2] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Declaring and Initializing Arrays

You can declare and fill an array in a single statement using curly braces, like int scores[] = {90, 85, 78};, and if you omit the size in the brackets, the compiler automatically counts the initializer values to determine it.

Specifying a size explicitly that's larger than your initializer list leaves the remaining elements zero-initialized.

Example: Declaring and Initializing Arrays

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int scores[] = {90, 85, 78};
	// Print to the console with cout
	std::cout << scores[0] << " " << scores[1] << " " << scores[2] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Accessing Array Elements

Array indexing is zero-based, meaning the first element sits at index 0 and the last at size - 1, not at size itself — attempting to access index size reads past the array's actual memory and produces undefined behavior rather than a helpful error.

This off-by-one boundary is one of the most common sources of bugs in array-heavy code.

Example: Accessing Array Elements

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int scores[] = {90, 85, 78};
	// Print to the console with cout
	std::cout << "First: " << scores[0] << std::endl;
	// Print to the console with cout
	std::cout << "Last: " << scores[2] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Modifying Array Elements

Assigning a new value directly to an indexed position, like scores[2] = 100;, permanently overwrites whatever was previously stored there — arrays don't preserve history, so once a value is replaced, the original is gone unless you saved it elsewhere first.

Example: Modifying Array Elements

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int scores[] = {90, 85, 78};
	scores[2] = 100;
	// Print to the console with cout
	std::cout << scores[2] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Looping Through Arrays

A standard indexed for loop or a range-based for loop can both walk through every element of an array in order; the indexed version additionally gives you the position of each element, which the range-based version does not expose directly.

Choose the indexed form when you actually need to know *where* an element is, not just its value.

Example: Looping Through Arrays

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int scores[] = {90, 85, 78};
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		// Print to the console with cout
		std::cout << scores[i] << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Reading or writing arr[5] in an array of size 5, when the valid indexes are 0 to 4, which is undefined behavior.
  2. Using an uninitialized local array and printing its elements, which shows garbage values.
  3. Trying to assign one array to another with a = b;, which is not allowed in C++; copy element by element instead.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.