Character Array s
A string literal can be stored in an array of characters.
Almost every type of data in C++ has a variable, but there is no variable for holding character strings. The authors of C++ realized that you need some way to store strings in variables, but instead of storing them in a string variable (as some languages such as BASIC or Pascal do) you must store them in an array of characters.
If you have never programmed before, an array might be new to you. An array is a list (sometimes called a table) of variables, and most programming languages allow the use of such lists. Suppose you had to keep track of the sales records of 100 salespeople. You could make up 100 variable names and assign a different salesperson’s sales record to each one.
All those different variable names, however, are difficult to track. If you were to put them in an array of floating-point variables, you would have to keep track of only a single name (the array name) and reference each of the 100 values by a numeric subscript.
The last few chapters of this book cover array processing in more detail. However, to work with character string data in your early programs, you have to become familiar with the concept of character arrays.
Because a string is simply a list of one or more characters, a character array is the perfect place to hold strings of information. Suppose you want to keep track of a person’s full name, age, and salary in variables. The age and salary are easy because there are variable types that can hold such data. The following code declares those two variables:
i nt age;
f l oat sal ar y;
You have no string variable to hold the name, but you can create an appropriate array of characters (which is actually one or more character variables in a row in memory) with the following declaration:
char name[ 15];
This reserves a character array. An array declaration always includes brackets ([] ) that declare the space for the array. This array is 15 characters long. The array name is name. You also can assign a
EXAMPLE
value to the character array at the time you declare the array. The following declaration statement not only declares the character array, but also assigns the name “Michael Jones” at the same time:
Declare the character array called name as 15 characters long, and assign
Mi chael Jones to the array.
char name[ 15] =” Mi chael Jones” ;
Figure 5.1 shows what this array looks like in memory. Each of the 15 boxes of the array is called an element. Notice the null zero (the string-terminating character) at the end of the string. Notice also that the last character of the array contains no data. You filled only the first 14 elements of the array with the data and the data’s null zero. The 15th element actually has a value in it—but whatever follows the string’s null zero is not a concern.
Figure 5.1. A character array after being declared and assigned a string value.
You can access individual elements in an array, or you can access the array as a whole. This is the primary advantage of an array over the use of many differently named variables. You can assign values to the individual array elements by putting the elements’ location, called a subscript, in brackets, as follows:
name[ 3] =’ k’ ;
Chapter 5 ♦ Character Arrays and Strings
This overwrites the h in the name Mi chael with a k. The string now looks like the one in Figure 5.2.
Figure 5.2. The array contents (see Figure 5.1) after changing one of the elements.
All array subscripts begin at0.
All array subscripts start at zero. Therefore, to overwrite the first element, you must use 0 as the subscript. Assigning name[ 3] (as is done in Figure 5.2) changes the value of the fourth element in the array.
You can print the entire string—or, more accurately, the entire array—with a single cout statement, as follows:
cout << name;
Notice when you print an array, you do not include brackets after the array name. You must be sure to reserve enough characters in the array to hold the entire string. The following line,
char name[ 5] =” Mi chael Jones” ;
is incorrect because it reserves only five characters for the array, whereas the name and its null zero require 14 characters. However,
C++ does give you an error message for this mistake (ill egal i ni t i al i zat i on ).
EXAMPLE
If your string contains 13 characters, it also must have a 14th for the null zero or it will never be treated like a string. To help eliminate this error, C++ gives you a shortcut. The following two character array statements are the same:
char hor se[ 9] =” St alli on” ;
and
char hor se[] =” St alli on” ;
If you assign a value to a character array at the same time you declare the array, C++ counts the string’s length, adds one for the null zero, and reserves the array space for you.
If you do not assign a value to an array at the time it is declared, you cannot declare it with empty brackets. The following statement,
char peopl e[];
does not reserve any space for the array called peopl e . Because you did not assign a value to the array when you declared it, C++ assumes this array contains zero elements. Therefore, you have no room to put values in this array later. Most compilers generate an error if you attempt this.