Beginner⏱️ 10 min📘 Topic 11 of 32

📋 Arrays in C++ — 1D, 2D, Initialization and Common Operations

Master C++ arrays — declare, initialize, iterate, pass to functions, multi-dimensional arrays and the modern std::array. Includes examples and interview Q&A.

An array is a fixed-size sequence of elements of the same type, stored in contiguous memory.

📜 Declaration

int nums[5];                   // 5 ints, uninitialized
int scores[5] = {90, 85, 70};  // first 3 set, rest are 0
int primes[]  = {2, 3, 5, 7};  // size inferred = 4

🔢 Access by index (0-based)

std::cout << primes[0]; // 2
std::cout << primes[3]; // 7

⚠️ No bounds checking

C++ won't stop you from accessing primes[100]. You'll read garbage or crash. The language trusts you to stay in range.

🟦 2D arrays — rows and columns

int grid[3][4] = {
  {1,2,3,4},
  {5,6,7,8},
  {9,10,11,12}
};
std::cout << grid[1][2]; // 7

🆕 std::array — modern, safer alternative

#include <array>
std::array<int, 5> nums = {1,2,3,4,5};
std::cout << nums.size(); // 5
std::cout << nums.at(2);  // 3 (bounds-checked)

💻 Code Examples

Sum of array elements

int nums[] = {3, 1, 4, 1, 5, 9, 2, 6};
int sum = 0;
for (int n : nums) sum += n;
std::cout << sum;
Output:
31

Find the maximum

int nums[] = {12, 7, 25, 9, 18};
int max = nums[0];
for (int i = 1; i < 5; i++) {
  if (nums[i] > max) max = nums[i];
}
std::cout << max;
Output:
25

Passing an array to a function

void printArray(int arr[], int size) {
  for (int i = 0; i < size; i++) std::cout << arr[i] << ' ';
}
int nums[] = {1,2,3};
printArray(nums, 3);
Output:
1 2 3

⚠️ Common Mistakes

  • Accessing out-of-bounds indices — no error, just undefined behavior.
  • Forgetting that arrays decay to pointers when passed to functions — `sizeof(arr)` inside the function gives the pointer's size, not the array's.
  • Trying to copy arrays with `=` — use std::copy or memcpy.
  • Hardcoding array sizes in many places — declare a const and reuse it.

🎯 Interview Questions

Real questions asked at top product and service-based companies.

Q1.Are C++ arrays 0-indexed or 1-indexed?Beginner
0-indexed. The first element is `arr[0]`, the last is `arr[size-1]`. Trying to access `arr[size]` is out-of-bounds.
Q2.What happens if you access an array out of bounds?Beginner
Undefined behavior — the program may read garbage, crash, or appear to work. C++ does not check bounds for raw arrays. `std::array::at()` and `vector::at()` DO throw `std::out_of_range`.
Q3.Why does sizeof(arr) inside a function not give the array size?Intermediate
When passed to a function, the array DECAYS to a pointer. `sizeof` on a pointer returns the pointer's size (8 bytes on 64-bit), not the array's total size. Pass size as a separate parameter or use std::array/vector.
Q4.What's the difference between a C-style array and std::array?Intermediate
C-style: raw, decays to pointer, no size info, no bounds check. std::array: wrapper class, knows its size, supports .at() / .size() / iteration, can be passed without losing info. Prefer std::array for fixed-size, std::vector for dynamic.
Q5.How are 2D arrays stored in memory?Advanced
Row-major order — all elements of row 0, then row 1, then row 2... so `grid[i][j]` is at offset `i * cols + j` from the start. This matters for cache-friendly iteration order: iterate row-first (i outer, j inner).

🧠 Quick Summary

  • Array = fixed-size, contiguous block of same-type values.
  • 0-indexed; no bounds checking — be careful.
  • Arrays decay to pointers when passed to functions.
  • 2D arrays are stored row-major in memory.
  • Prefer std::array (fixed) or std::vector (dynamic) in modern code.