7.4.3. Specifying an array’s Size with a Constant Variable and Setting array Elements with Calculations

Figure 7.5 sets the elements of a 5-element array s to the even integers 2, 4, 6, 8 and 10 (lines 15–16) and prints the array in tabular format (lines 18–22). These numbers are generated (line 16) by multiplying each successive value of the loop counter by 2 and adding 2.


 1   // Fig. 7.5: fig07_05.cpp
 2   // Set array s to the even integers from 2 to 10.
 3   #include <iostream>
 4   #include <iomanip>
 5   #include <array>
 6   using namespace std;
 7
 8   int main()
 9   {
10      // constant variable can be used to specify array size
11      const size_t arraySize = 5; // must initialize in declaration
12
13      array< int, arraySize > s; // array s has 5 elements
14
15      for ( size_t i = 0; i < s.size(); ++i ) // set the values
16         s[ i ] = 2 + 2 * i;                                   
17
18      cout << "Element" << setw( 13 ) << "Value" << endl;
19
20      // output contents of array s in tabular format
21      for ( size_t j = 0; j < s.size(); ++j )
22         cout << setw( 7 ) << j << setw( 13 ) << s[ j ] << endl;
23   } // end main


Element        Value
      0            2
      1            4
      2            6
      3            8
      4           10


Fig. 7.5. Set array s to the even integers from 2 to 10.

Line 11 uses the const qualifier to declare a constant variable arraySize with the value 5. A constant variable that’s used to specify array’s size must be initialized with a constant expression when it’s declared and cannot be modified thereafter (as shown in Fig. 7.6 and Fig. 7.7). Constant variables are also called named constants or read-only variables.


 1   // Fig. 7.6: fig07_06.cpp
 2   // Using a properly initialized constant variable.
 3   #include <iostream>
 4   using namespace std;
 5
 6   int main()
 7   {
 8      const int x = 7; // initialized constant variable
 9
10      cout << "The value of constant variable x is: " << x << endl;
11   } // end main


The value of constant variable x is: 7


Fig. 7.6. Using a properly initialized constant variable.


 1   // Fig. 7.7: fig07_07.cpp
 2   // A const variable must be initialized.
 3
 4   int main()
 5   {
 6      const int x; // Error: x must be initialized
 7
 8      x = 7; // Error: cannot modify a const variable
 9   } // end main

Microsoft Visual C++ compiler error message:


error C2734: 'x' : const object must be initialized if not extern
error C3892: 'x' : you cannot assign to a variable that is const


GNU C++ compiler error message:


fig07_07.cpp:6:14: error: uninitialized const 'x' [-fpermissive]
fig07_07.cpp:8:8: error: assignment of read-only variable 'x'


LLVM compiler error message:


Default initialization of an object of const type 'const int'


Fig. 7.7. A const variable must be initialized.


Image Common Programming Error 7.1

Not initializing a constant variable when it’s declared is a compilation error.



Image Common Programming Error 7.2

Assigning a value to a constant variable in an executable statement is a compilation error.


In Fig. 7.7, the compilation error produced by Microsoft Visual C++ refers to the int variable x as a “const object.” The C++ standard defines an “object” as any “region of storage.” Like class objects, fundamental-type variables also occupy space in memory, so they’re often referred to as “objects.”

Constant variables can be placed anywhere a constant expression is expected. In Fig. 7.5, constant variable arraySize specifies the size of array s in line 13.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.196.223