Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟡 Control Flow  ·  Lesson 21

Arrays in C++

In this lesson: Declare, initialise and loop arrays in C++ — 1D, 2D, passing to functions — with output. In Hindi & English.

What is an array?

An array stores many values of the same type in one named block, reached by an index. Instead of m1, m2, m3, m4, m5, you write int marks[5]; and use marks[0] to marks[4].

Declaring and initialising

C++
int a[5];                     // five ints, uninitialised
int b[5] = {1, 2, 3, 4, 5};   // with values
int c[5] = {1, 2};            // rest become 0
int d[]  = {10, 20, 30};      // size inferred as 3

Accessing elements

Indexes run from 0 to size − 1. The first is a[0]; the last of a 5-element array is a[4].

C++
int b[5] = {1, 2, 3, 4, 5};
cout << b[0] << " " << b[4] << "\n";   // 1 5
b[2] = 99;                              // change an element

Looping through an array

C++
int b[5] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 5; i++) sum += b[i];
cout << "Sum = " << sum << "\n";

for (int x : b) cout << x << " ";   // range-based for
Dry Run — dekhiye har step kaise chalta hai (i = 0 se shuru)
Stepi ki valueConditionKya huai ab
100 < 5 → trueloop body chalta haii → 1
211 < 5 → trueloop body chalta haii → 2
322 < 5 → trueloop body chalta haii → 3
433 < 5 → trueloop body chalta haii → 4
544 < 5 → trueloop body chalta haii → 5
655 < 5 → falseCondition false — loop yahin ruk gaya (STOP)—
Output:
Sum = 15
1 2 3 4 5

2D arrays

A 2D array is a grid with two indexes — row and column:

C++
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) cout << grid[i][j] << " ";
    cout << "\n";
}
Output:
1 2 3
4 5 6

A note on std::vector

Built-in arrays have a fixed size and do not track their own length. For most real code, C++ programmers prefer std::vector, which grows as needed and knows its .size(). See the vector lesson.

💡 Rule of thumb

Learn arrays to understand the basics, but reach for std::vector when you actually build programs.

Common mistakes

  • Going out of bounds — using a[5] on a 5-element array.
  • Assuming an array knows its length inside a function (it does not).
  • Looping with <= size instead of < size.
  • Forgetting that uninitialised array elements hold garbage.
🏋️ Practice

Read five numbers into an array, then print the largest and the average. Then make a 3×3 grid and print its diagonal (grid[0][0], grid[1][1], grid[2][2]).

Summary

  • An array holds many same-type values, accessed by index from 0.
  • Initialise with braces; missing values become 0.
  • Loop with a classic for or a range-based for.
  • 2D arrays use two indexes for rows and columns.
  • Prefer std::vector for real programs — it is safer and resizable.

Frequently Asked Questions

What is an array in C++?
An array is a collection of elements of the same type stored in consecutive memory, accessed by an index. For example int marks[5]; holds five integers, reached with marks[0] through marks[4]. Its size is fixed when you declare it.
How do you declare and initialise an array in C++?
You give a type, name and size, such as int a[5];, and can initialise with braces at the same time, like int a[5] = {1, 2, 3, 4, 5};. If you supply fewer values than the size, the remaining elements are set to zero.
Why do array indexes start at 0 in C++?
An array index is really an offset from the start of the array, so the first element sits at offset 0. That is why a[0] is the first item and the last item of an array of size n is a[n-1].
What is the difference between an array and a vector in C++?
A built-in array has a fixed size set at compile time and does not know its own length. A std::vector can grow and shrink at run time, knows its size with .size(), and is generally safer and more convenient, which is why modern C++ often prefers it.
What happens if you access an array out of bounds in C++?
C++ does not check array bounds, so reading or writing past the end is undefined behaviour — it may return garbage, corrupt other data, or crash. You must keep every index between 0 and the array size minus one.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicStrings in C++ Your progress: 21 / 81 (26%) · Lesson 22 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free