100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Arrays in C++

Learn what an array is in C++, how to declare and access a fixed-size collection of same-type elements, and why zero-based indexing matters.

ArraysBeginner6 min readJul 7, 2026
Analogies

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

cpp
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

cpp
#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

text
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 0 to arraySize - 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

Was this page helpful?

Topics covered

#CStudyNotes#Programming#ArraysInC#Arrays#Syntax#Explanation#Example#DataStructures#StudyNotes#SkillVeris