C++20 sections · 1024 units
Open in Course

Structs with Arrays

Arrays inside structs

Put arrays inside structs just like any other member. The array becomes part of the struct. Every variable of that type gets its own copy of the entire array. Example: struct Scores { int grades[5]; }; defines a type with an array of 5 integers.

Create Scores s; and access with s.grades[0] through s.grades[4]. The entire array lives inside the struct. If you copy the struct variable, you copy the whole array. This differs from standalone arrays, which decay to pointers.