Concepts / Working with fixed-size sequences using array

Working with fixed-size sequences using array

An array can be initialized by an initializer list, which is a braced list of values written in the declaration.

  • Programming

Starting Values Inside the Declaration

When you create an array, you often already know what its contents should be. One option is to declare an empty container and then assign each slot one at a time. A more direct option is to supply the starting values together with the declaration itself. The source material for this topic calls this technique initializing an array with an initializer list.

Before reading further, look at a declaration and ask yourself three questions: what name refers to the array, how many values the declaration asks for, and where those values come from. The rest of this article answers each of those questions and then gives you a rule for deciding whether a declaration is valid.

cpp
Notes on the code
  • a1 is the name you use to refer to the array later
  • int is the element type, and 3 is the declared number of elements
  • the braced list supplies the starting values

Reading the Parts of the Declaration

The declaration has three parts. The name a1 is the identifier you will use to refer to the array later in the program. The type portion array<int,3> carries two pieces of information: the element type int, and the number 3, which is the number of elements the array is specified to hold. The part after the equals sign, enclosed in braces, is the initializer list, the list of values that fill the array when it is created.

array<int, 3>element type int, declaredsize 3a1name used to refer to thearray=starts the initialization{10, 20, 30}initializer list ofsupplied values
What does each part of the declaration contribute to the array: the type, the name, the declared size, and the braced values?

Keep the two numbers in the type separate in your mind. The 3 in array<int,3> is the declared size, which is a property of the array itself. The values inside the braces are the supplied values, which are what you are asking the declaration to store. The counting rule in the next section compares these two quantities.

The Counting Rule for Initializers

Definition

The number of elements in an initializer list must be equal to or less than the number of elements specified for the array. In symbols, if n is the count of values in the braces and N is the declared size, the declaration is allowed when $n \leq N$.

Notice that this rule is a comparison, not an exact match. An initializer with exactly the declared number of values passes, and an initializer with fewer values also passes. Only a count larger than the declared size breaks the rule. So for array<int,3>, the braces may hold three values, two values, one value, or none at all, but never four.

Where Each Value Lands

When an initializer list matches the declared size, each value in the braces occupies one slot of the array. The first value sits in the first position, the second in the second, and so on. Counting from zero, which is the common convention for C++ arrays, the first value goes to position 0, the second to position 1, and the third to position 2. Later code uses these positions to read or change individual elements.

a1[0]10a1[1]20a1[2]30next positionnext position
Which supplied value lands in which element position, and what index is each one stored at?

Read the diagram from left to right. The value 10 is the first one written in the braces, so it occupies position 0. The value 20 occupies position 1, and the value 30 occupies position 2. The diagram shows the three slots that the declaration sets up for a1, with each slot holding one value in the order the values were written.

Checking a Declaration for Violations

To check a declaration, count the values in the braces and compare that count with the declared size. If the count is less than or equal to the size, the declaration satisfies the rule. If the count is larger, the declaration breaks the rule, and the problem sits in the declaration line, so that is where you should look first.

Read thedeclarationCount values inbracesCount at mostdeclared size?Valid declarationViolation ondeclaration lineyesno
What check is performed on the value count against the declared size, and at which line does a failing check occur?
Worked example

Testing two declarations against the size rule

Decide whether each declaration satisfies the rule, using array<int,3> as the declared size in both cases: first array<int, 3> a1 = {4, 8, 15}; and then array<int, 3> a2 = {4, 8, 15, 16};

  1. 1
    Identify the declared size

    Both declarations use array<int, 3>, so the declared size N is 3.

  2. 2
    Count the values in the first declaration

    The braces {4, 8, 15} contain 3 values, so n is 3.

  3. 3
    Compare for the first declaration

    3 is equal to 3, so n is less than or equal to N. The first declaration is valid.

  4. 4
    Count the values in the second declaration

    The braces {4, 8, 15, 16} contain 4 values, so n is 4.

  5. 5
    Compare for the second declaration

    4 is larger than 3, so the rule is broken. The problem is on the second declaration line, which is where you should look first.

Result

The declaration for a1 is valid. The declaration for a2 violates the rule, and the violation is on the line that declares a2.

cpp
Notes on the code
  • the first line matches the declared size exactly
  • the second line supplies one more value than the declared size allows

Common Mistakes with Initializer Lists

  • Common mistake

    Treating a smaller count as an error

    What it looks like
    array<int, 3> a1 = {4, 8};
    Why it fails

    The rule allows counts equal to or less than the declared size, so two values in a size-3 array is permitted.

    Do this instead

    Only flag a declaration when the count of values in the braces is greater than the declared size.

  • Common mistake

    Reading the declared size from the braces

    What it looks like
    array<int, 3> a1 = {4, 8, 15, 16};
    Why it fails

    The 3 belongs to the type, array<int,3>, and it is not produced by counting the braced values.

    Do this instead

    Read the size from the type portion of the declaration, then compare it with the count of braced values.

  • Common mistake

    Looking for the error in the later code that uses the array

    Why it fails

    The source states that when a declaration violates the counting rule, the problem sits in the declaration line.

    Do this instead

    Go to the declaration line first and check the count of values there.

Predict Before You Check

What do you think happens?

Will this declaration satisfy the counting rule: array<int, 3> a3 = {1, 2, 3, 4, 5};

  • Yes, it is valid because the array has a declared size of 3

  • No, it is invalid because it supplies 5 values to an array of declared size 3

Reveal answer
Answer:

No, it is invalid because it supplies 5 values to an array of declared size 3

The rule requires the number of values in the braces to be equal to or less than the declared size. Here 5 is larger than 3, so the rule is broken, and the problem sits on this declaration line.

EASY

For each declaration, first identify the declared size, then count the braced values, then decide whether the rule holds. Try array<int, 4> b1 = {7, 9}; and array<int, 4> b2 = {7, 9, 11, 13, 15};. Which declaration is valid, and on which line would a violation appear?

Hints
  • The declared size of b1 and b2 is 4, which comes from the type portion array<int, 4>.
  • Compare the count of braced values with 4 using equal to or less than.

Key Takeaways

  • An initializer list is a braced list of values written in an array declaration that fills the array when it is created.
  • The counting rule requires the number of values in the initializer to be equal to or less than the declared size, so an exact match and a smaller count both pass, but a larger count fails.
  • Each initializer value occupies one position, counting from zero, with the first value at position 0.
  • When a declaration violates the counting rule, check the declaration line first, because that is where the problem sits.

Related concepts

More concepts starting with W