C++ Arrays Introduction
In this page:
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?
#include <iostream>
int main() {
int scores[3] = {90, 85, 78};
std::cout << scores[0] << " " << scores[1] << " " << scores[2] << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
int main() {
int scores[] = {90, 85, 78};
std::cout << scores[0] << " " << scores[1] << " " << scores[2] << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
int main() {
int scores[] = {90, 85, 78};
std::cout << "First: " << scores[0] << std::endl;
std::cout << "Last: " << scores[2] << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
int main() {
int scores[] = {90, 85, 78};
scores[2] = 100;
std::cout << scores[2] << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
int main() {
int scores[] = {90, 85, 78};
for (int i = 0; i < 3; i++) {
std::cout << scores[i] << " ";
}
std::cout << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- C++ Arrays Introduction
- C++ Looping Through Arrays
- C++ Omitting Array Size
- C++ Array Size
- C++ Multi-dimensional Arrays
- C++ Arrays & Functions
- C++ Strings (C-style)
- C++ std::string
- C++ String Concatenation
- C++ Converting Strings and Numbers
- C++ String Length
- C++ Accessing String Characters
- C++ using namespace std
- C++ String Methods
- C++ Array of Strings