codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

25 / 87 topics
25Arrays (1D and Multidimensional)26Passing Array to a Function27Strings & the C++ String Class
Tutorials/C++ Programming/Arrays (1D and Multidimensional)
⚡C++ Programming

Arrays (1D and Multidimensional)

Updated 2026-05-12
30 min read

Arrays (1D and Multidimensional)

Arrays are fundamental data structures in C++ that allow you to store multiple values of the same type in a single variable. They provide an efficient way to manage collections of data, making them essential for various programming tasks such as handling lists, matrices, and more.

In this tutorial, we'll explore 1D arrays (one-dimensional arrays) and multidimensional arrays, including how to declare, initialize, access elements, determine array size, and understand the concept of array decay. By the end of this lesson, you'll have a solid understanding of how to work with arrays in C++.

Declaring and Initializing 1D Arrays

A 1D array is essentially a list of elements stored in contiguous memory locations. You can declare an array by specifying its type and size, or by initializing it directly.

Declaration

To declare a 1D array, you need to specify the data type followed by the array name and the number of elements in square brackets:

declare_array.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int numbers[5]; // Declare an array of 5 integers
6
7 return 0;
8}

Initialization

You can initialize an array at the time of declaration by providing a list of values enclosed in curly braces:

initialize_array.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int numbers[5] = {1, 2, 3, 4, 5}; // Initialize an array with 5 integers
6
7 return 0;
8}

If you provide fewer initializers than the size of the array, the remaining elements are automatically initialized to zero:

partial_initialization.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int numbers[5] = {1, 2}; // Initialize first two elements; others are set to 0
6
7 return 0;
8}

Accessing Array Elements

Array elements are accessed using their index, which starts from 0. You can access and modify individual elements as follows:

access_array_elements.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int numbers[5] = {1, 2, 3, 4, 5};
6
7 cout << "First element: " << numbers[0] << endl; // Output: 1
8 cout << "Third element: " << numbers[2] << endl; // Output: 3
9
10 numbers[1] = 10; // Modify the second element
11 cout << "Modified second element: " << numbers[1] << endl; // Output: 10
12
13 return 0;
14}

Multidimensional Arrays

Multidimensional arrays are arrays of arrays. The most common type is a two-dimensional array, which can be visualized as a table or matrix.

Declaration and Initialization

To declare a 2D array, you specify the data type followed by two sets of square brackets representing rows and columns:

declare_2d_array.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int matrix[3][4]; // Declare a 3x4 matrix
6
7 return 0;
8}

You can initialize a 2D array using nested curly braces:

initialize_2d_array.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int matrix[3][4] = {
6 {1, 2, 3, 4},
7 {5, 6, 7, 8},
8 {9, 10, 11, 12}
9 };
10
11 return 0;
12}

Accessing Elements

Access elements in a 2D array using two indices: the row index and the column index:

access_2d_array_elements.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int matrix[3][4] = {
6 {1, 2, 3, 4},
7 {5, 6, 7, 8},
8 {9, 10, 11, 12}
9 };
10
11 cout << "Element at row 1, column 2: " << matrix[1][2] << endl; // Output: 7
12
13 return 0;
14}

Array Size and Decay

Determining Array Size

The size of an array can be determined using the sizeof operator. However, note that this returns the total size in bytes, not the number of elements:

array_size.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int numbers[5] = {1, 2, 3, 4, 5};
6
7 cout << "Total size in bytes: " << sizeof(numbers) << endl; // Output depends on the system
8 cout << "Number of elements: " << sizeof(numbers) / sizeof(numbers[0]) << endl; // Output: 5
9
10 return 0;
11}

Array Decay

In C++, when you pass an array to a function, it decays into a pointer to its first element. This means that the size information is lost:

array_decay.cpp
1#include <iostream>
2using namespace std;
3
4void printArraySize(int arr[]) {
5 cout << "Size of array in function: " << sizeof(arr) << endl; // Output depends on the system
6}
7
8int main() {
9 int numbers[5] = {1, 2, 3, 4, 5};
10
11 cout << "Size of array in main: " << sizeof(numbers) << endl; // Output depends on the system
12
13 printArraySize(numbers);
14
15 return 0;
16}

Warning

Be cautious when passing arrays to functions. The size information is lost, so you need to pass it explicitly if needed.

Practical Example: Matrix Multiplication

Let's create a practical example that multiplies two matrices using 2D arrays:

matrix_multiplication.cpp
1#include <iostream>
2using namespace std;
3
4void multiplyMatrices(int A[3][3], int B[3][3], int result[3][3]) {
5 for (int i = 0; i < 3; ++i) {
6 for (int j = 0; j < 3; ++j) {
7 result[i][j] = 0;
8 for (int k = 0; k < 3; ++k) {
9 result[i][j] += A[i][k] * B[k][j];
10 }
11 }
12 }
13}
14
15void printMatrix(int matrix[3][3]) {
16 for (int i = 0; i < 3; ++i) {
17 for (int j = 0; j < 3; ++j) {
18 cout << matrix[i][j] << " ";
19 }
20 cout << endl;
21 }
22}
23
24int main() {
25 int A[3][3] = {
26 {1, 2, 3},
27 {4, 5, 6},
28 {7, 8, 9}
29 };
30
31 int B[3][3] = {
32 {9, 8, 7},
33 {6, 5, 4},
34 {3, 2, 1}
35 };
36
37 int result[3][3];
38
39 multiplyMatrices(A, B, result);
40
41 cout << "Result of matrix multiplication:" << endl;
42 printMatrix(result);
43
44 return 0;
45}
Output
Result of matrix multiplication:
30 24 18 
84 69 54 
138 114 90

Summary

  • 1D Arrays: Store a list of elements in contiguous memory locations.
  • Initialization: Can be done at declaration using curly braces.
  • Accessing Elements: Use indices starting from 0.
  • Multidimensional Arrays: Arrays of arrays, commonly used for matrices.
  • Array Size: Use sizeof to determine the total size in bytes; divide by element size to get the number of elements.
  • Array Decay: When passed to a function, arrays decay into pointers, losing size information.

What's Next?

In the next topic, we'll explore how to pass arrays to functions and handle them properly. Understanding array decay is crucial for working with arrays in functions, so make sure you're comfortable with these concepts before moving on.


PreviousLambda ExpressionsNext Passing Array to a Function

Recommended Gear

Lambda ExpressionsPassing Array to a Function