C++ - Arrays

Overview

Estimated time: 30–40 minutes

Store fixed-size sequences in stack-allocated arrays. Learn initialization, sizing, and iteration.

Learning Objectives

  • Declare and initialize arrays.
  • Get array size with std::size or sizeof.

Prerequisites

Examples

#include 
#include 
int a[3] = {1,2,3};
for (int i=0;i<3;++i) std::cout << a[i] << ' ';

Size

#include 
int n = std::size(a); // C++17

Common Pitfalls

  • Arrays have fixed size; prefer std::vector for dynamic sizing.