Arrays in C++ – Complete Tutorial (Beginner to Advanced)
Last Updated on: 10th Dec 2025 12:38:58 PM
An array in C++ is one of the most fundamental data structures used for storing multiple values of the same data type under a single variable name. Arrays make data management easier, faster, and more structured, especially when dealing with large sets of data like marks of students, prices of products, or elements in algorithms.
What is an Array in C++?
An array is a collection of elements of the same data type stored at contiguous memory locations, accessed using a single variable name with an index.
✔ Key Points
-
Stores multiple data items of the same type
-
Stored in continuous (contiguous) memory
-
Index always starts from 0
-
Fast access using index
-
Useful for managing large datasets
Example
int numbers[5] = {10, 20, 30, 40, 50};
Why Do We Need Arrays?
Without arrays, storing 100 integers would require:
int a1, a2, a3, ... a100;
This is unreadable and unmanageable.
With arrays:
int a[100];
Much easier for:
-
loops
-
operations
-
searches
-
sorting
-
mathematical calculations
Memory Representation of an Array
Example:
int arr[4] = {5, 10, 15, 20};
| Index | Value | Memory Address |
|---|---|---|
| 0 | 5 | 1000 |
| 1 | 10 | 1004 |
| 2 | 15 | 1008 |
| 3 | 20 | 1012 |
(Assuming each int = 4 bytes)
📝 All memory locations are continuous, that’s why array operations are fast.
4. Types of Arrays in C++
C++ mainly supports:
-
One-Dimensional Array (1D)
-
Two-Dimensional Array (2D)
-
Multi-Dimensional Array
-
Character Arrays (Strings)
5. One-Dimensional Array (1D Array)
A one-dimensional array stores a list of values in a single row.
Syntax
data_type array_name[size];
Example
int marks[5];
6. Initialization of 1D Array
At Declaration Time
int arr[5] = {10, 20, 30, 40, 50};
Without Size
int arr[] = {1, 2, 3, 4};
7. Accessing Array Elements
Syntax
array_name[index];
index start from 0
cout << arr[0]; // First element
8. Input and Output of Array Using Loop
Example
#include <iostream>
using namespace std;
int main() {
int arr[5];
cout << "Enter 5 elements:\n";
for (int i = 0; i < 5; i++) {
cin >> arr[i];
}
cout << "Array elements are:\n";
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
return 0;
}
9. Common Operations on Arrays
1. Traversing
Accessing each element using loops.
2. Insertion
Adding element at a specific index (manually shifting).
3. Deletion
Removing element and shifting remaining elements.
4. Searching
-
Linear Search
-
Binary Search (on sorted array)
5. Sorting
-
Bubble Sort
-
Selection Sort
-
Insertion Sort
10. Passing Array to Function
Arrays are passed to functions by reference (address).
Example
void display(int arr[], int n) {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
int main() {
int a[3] = {1, 2, 3};
display(a, 3);
}
11. Two-Dimensional Array (2D Array)
A 2D array stores data in rows and columns (matrix form).
Syntax
data_type array_name[row][column];
Example
int marks[3][3];
12. Initialization of 2D Array
int arr[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
13. Input & Output of 2D Array
#include <iostream>
using namespace std;
int main() {
int a[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cin >> a[i][j];
}
}
cout << "Matrix:\n";
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
}
14. Multi-Dimensional Arrays
Arrays with more than two dimensions.
int arr[3][4][2];
Used rarely; mostly in:
-
Graphics
-
Scientific computing
-
Tensor calculations
15. Character Arrays (Strings)
A character array stores characters ending with '\0' (null character).
char name[10] = "C++";
16. String Input Example
char name[20];
cin >> name;
cout << name;
or using cin.getline():
cin.getline(name, 20);
17. Array Indexing Rules
1. Index starts from 0
2. Last index = size - 1
❌ Out-of-bound access causes runtime error
19. Advantages of Arrays
1. Easy data management
2. Fast access
3. Useful for sorting/searching
4. Works well with loops
20. Limitations of Arrays
1. Fixed size
2. Cannot grow dynamically
3. Insertion and deletion costly
4. Homogeneous data only
Note : These limitations lead to Vectors, Linked Lists, and STL containers.
21. Array vs Variables
| Feature | Variable | Array |
|---|---|---|
| Stores | Single value | Multiple values |
| Access | Direct | Indexed |
| Memory | Single | Contiguous block |
22. Common Mistakes
1. Accessing out-of-bound index
2. Using wrong loop condition
3. Forgetting array size
4. Confusing index and size
23. Interview-Important Programs
1. Find largest & smallest element
2. Reverse an array
3. Sum and average
4. Sorting array
5. Searching element
6. Matrix addition & multiplication
24. Real-World Applications
-
Student management systems
-
Inventory systems
-
Data analysis
-
Game development
-
Image processing
Conclusion
Arrays in C++ are a powerful structure for storing and processing collections of data. They are fast, easy to use, and essential for loops, algorithms, data structures, and real-world applications. Mastering arrays is the first step to mastering data structures in C++.
✅ Strong array knowledge = strong programming base
Keep practicing — you're doing amazing!
Happy Coding! ![]()