C arrays and pointer decay
A built-in (C-style) array is a fixed number of elements of one type, stored side by side in memory: int marks[5];. The size must be a compile-time constant and can never change. Indexes start at 0, so the last element of a 5-element array is marks[4]. Reading or writing past the end is not checked: it is undefined behaviour that may corrupt other variables silently.
#include <iostream>
#include <iterator>
int main() {
int marks[5] = {72, 88, 95, 60, 81};
double prices[] = {9.5, 12.0, 3.25}; // size deduced: 3
int zeros[4] = {}; // all four set to 0
int partial[4] = {1, 2}; // rest become 0: {1, 2, 0, 0}
std::cout << "first " << marks[0] << ", last " << marks[4] << '\n';
std::cout << "prices has " << std::size(prices) << " elements\n";
std::cout << "bytes: " << sizeof(marks) << '\n'; // 5 * 4
marks[3] += 10;
int total = 0;
for (int m : marks) total += m;
std::cout << "total " << total << '\n';
std::cout << zeros[3] << ' ' << partial[1] << ' ' << partial[3] << '\n';
}first 72, last 81
prices has 3 elements
bytes: 20
total 406
0 2 0Change int zeros[4] = {}; to int zeros[4]; inside main and print it. Local arrays without an initializer hold garbage, just like single variables.
The catch comes when you pass an array to a function. The array is not copied; it decays into a pointer to its first element, and the size is lost. Inside the function, int arr[] really means int* arr. That is why C-style code always passes the length as a separate parameter.
sizeof on array function parameter will return size of 'int *'
#include <iostream>
void printSize(int arr[]) {
std::cout << sizeof(arr) << '\n';
}
int main() {
int nums[5] = {1, 2, 3, 4, 5};
printSize(nums);
}main.cpp:4:24: warning: sizeof on array function parameter will return size of 'int *' instead of 'int[]' [-Wsizeof-array-argument]
4 | std::cout << sizeof(arr) << '\n';
| ^
main.cpp:3:20: note: declared here
3 | void printSize(int arr[]) {
| ^The program compiles and prints 8: the size of a pointer on a 64-bit machine, not 20 bytes of array. Any loop bound computed as sizeof(arr) / sizeof(arr[0]) inside the function is therefore wrong (it gives 2, not 5).
Pass a container that knows its size: a std::array, a std::vector, or a std::span (C++20) that views any contiguous sequence.
#include <iostream>
#include <span>
void printSize(std::span<const int> arr) {
std::cout << arr.size() << '\n'; // 5
}
int main() {
int nums[5] = {1, 2, 3, 4, 5};
printSize(nums);
}array type is not assignable
int main() {
int a[3] = {1, 2, 3};
int b[3];
b = a;
}main.cpp:4:7: error: array type 'int[3]' is not assignable
4 | b = a;
| ~ ^Built-in arrays cannot be copied with =, compared with == or returned from functions. It is a limitation inherited from C, and one of the reasons std::array exists.
Use std::array, which copies, compares and returns like any other value.
#include <array>
int main() {
std::array<int, 3> a{1, 2, 3};
std::array<int, 3> b = a; // element-by-element copy
return b == a ? 0 : 1; // and comparison works too
}