1. Intro
An array in C++ is a fixed-size, contiguous collection of elements that all share the same data type. Instead of creating separate variables marks1, marks2, marks3 for a student's scores, you can store them all in a single array marks and access each value using an index. Arrays are one of the most fundamental data structures in C++ and form the basis for strings, matrices, and many standard library containers.
Cricket analogy: Instead of tracking individual variables for each of a team's 11 batting scores, a scorer uses one array scores[11] indexed by batting position, just as C++ arrays replace separate variables like marks1, marks2, marks3 with a single indexed collection.
2. Syntax
dataType arrayName[arraySize];
// Example
int marks[5];3. Explanation
The declaration int marks[5]; reserves memory for 5 contiguous int values. arraySize must be a constant expression known at compile time for a plain (non-dynamic) array. Every element in the array occupies the same amount of memory and is stored right next to the previous one, which is what makes array access extremely fast. Elements are accessed using an index in square brackets, and C++ uses zero-based indexing — the first element is marks[0], not marks[1], and the last valid index is arraySize - 1. Assigning or reading a value uses the same arrayName[index] syntax, for example marks[0] = 90; stores 90 in the first slot.
Cricket analogy: Declaring int marks[5]; reserves 5 contiguous memory slots the way a scorer pre-prints exactly 5 batting-position rows on a card; the opener occupies row marks[0], not marks[1], since C++ uses zero-based indexing, and marks[0] = 90; is like writing '90' into the opener's row.
4. Example
#include <iostream>
using namespace std;
int main() {
int marks[5] = {90, 85, 78, 92, 88};
cout << "First element: " << marks[0] << endl;
cout << "Last element: " << marks[4] << endl;
cout << "All marks: ";
for (int i = 0; i < 5; i++) {
cout << marks[i] << " ";
}
cout << endl;
return 0;
}5. Output
First element: 90
Last element: 88
All marks: 90 85 78 92 88 6. Key Takeaways
- An array is a fixed-size, contiguous block of memory holding elements of the same
dataType. - Indexing is zero-based: valid indices run from
0toarraySize - 1. - Array size is normally fixed at compile time and cannot grow or shrink once declared.
- Because elements are stored contiguously, accessing any element by index is a fast, constant-time operation.
Practice what you learned
1. What is the index of the first element in a C++ array?
2. Given `int arr[5];`, what is the index of the last valid element?
3. Which of the following best describes how array elements are stored in memory?
4. Can all elements of a C++ array have different data types?
5. What does `marks[2]` refer to in `int marks[5] = {90, 85, 78, 92, 88};`?
Was this page helpful?
You May Also Like
Multidimensional Arrays in C++
Understand multidimensional arrays in C++ as arrays of arrays, how 2D arrays model rows and columns, and how row-major storage works.
Array Initialization in C++
Learn the different ways to initialize a C++ array at declaration, including partial initialization, zero-filling, and omitting the size.
Passing Arrays to Functions in C++
Understand how arrays decay to pointers when passed to C++ functions, why size must be passed separately, and how this differs from pass-by-value.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics