C++ Arrays


What Is an Array in C++?

An array in C++ is a container that holds several values of the same type using a single name. It's like having multiple drawers labeled with one tag, where each drawer can hold a different value — but all values are of the same kind (like all integers or all characters).


Why Use Arrays?

  • Organizes similar values under one identifier
  • Makes it simple to loop through multiple elements
  • Offers fast access using index numbers
  • Reduces the need for repeated variable declarations
Instead of creating five separate variables (int a, b, c, d, e), one array like int nums[5]; stores them all under one label.

Array Declaration Syntax

type arrayName[size];
  • type: The data kind you want to store (e.g., int, float, char)
  • arrayName: The label you assign to the array
  • size: Total number of slots it can hold

Example

#include <iostream>  

int main() {     
      int scores[3] = {85, 90, 78};      

     for (int i = 0; i < 3; i++) {         
            std::cout << "Score " << i + 1 << ": " << scores[i] << "\n";    
     }      

    Return 0; 
} 

Explanation

  • int scores[3]: Creates an array for three numbers.
  • {85, 90, 78}: These are the initial stored values.
  • The for loop displays each item using its position.
  • scores[0] gives 85, scores[1] gives 90, and so on.

Array Indexing

  • Arrays start counting from zero.
  • The first element is at index 0, not 1.
  • Accessing an element uses square brackets like arrayName[position].
  • Trying to access beyond the declared size can cause unexpected behavior (undefined or memory errors).

Types of Arrays

TypeUnique Description
One-DimensionalA straight list of items like [1, 2, 3]; works like a row of lockers.
Multi-DimensionalArrays inside arrays; for example, a grid or table of numbers (like matrix[2][3]).
Character ArraysStore letters or words, often used for C-style strings.

2D Array Example

#include <iostream>  

int main() {     
      int grid[2][2] = {         
            {1, 2},         
            {3, 4}    
      };      
 
     for (int row = 0; row < 2; row++) {         
          for (int col = 0; col < 2; col++) {             
                  std::cout << grid[row][col] << " ";         
           }         
           std::cout << "\n";     
     }      

     return 0; 
} 

Explanation

  • grid[2][2] sets up a square with two rows and two columns.
  • Nested for loops cycle through each spot.

Values shown:

1 2   
3 4 

Character Array Example

#include <iostream>  

int main() {     
       char word[6] = {'H', 'e', 'l', 'l', 'o', '\0'};     
       std::cout << word;     
      return 0; 
} 
  • This holds letters forming "Hello".
  • \0 is the null character, marking the end of the text.

Summary

Arrays in C++ give you the power to manage collections of data using structured, compact, and consistent logic. Whether you’re handling scores, letters, or a full matrix — arrays let you group and control similar data under one roof. They're fast, clean, and essential for any serious coding in C++.


Prefer Learning by Watching?

Watch these YouTube tutorials to understand C++ Tutorial visually:

What You'll Learn:
  • 📌 Learn C++ With Me #9 - Arrays
  • 📌 Arrays in C++
Previous Next