Declaring Strings
This section teaches you how to declare and initialize strings, as well as the difference between string constants and character constants. First, let's review the definition of a string.
What Is a String?
As introduced in Hour 12, "Storing Similar Data Items," a string is a character array terminated by a null character (\0).
For instance, a character array, array_ch, declared in the following statement, is considered a character string:
char array_ch[7] = {`H', `e', `l', `l', `o', `!', `\0'};
In C, the null character can be used to mark the end of a string, or to return logical FALSE. C treats \0 as one character. Each character in a string takes only 1 byte.
A series of characters enclosed in double quotes ("") is called a string constant. The C compiler can automatically add a null character (\0) at the end of a string constant to indicate the end of the string.
For example, the character string "A character string." is considered a string constant; so is "Hello!".
Initializing Strings
As taught in the last lesson, a character array can be declared and initialized like this:
char arr_str[6] = {`H', `e', `l', `l', `o', `!'};
Here the array arr_str is treated as a character array. However, if you add a null character (\0) into the array, you can have the following statement:
char arr_str[7] = {`H', `e', `l', `l', `o', `!', `\0'};
Here the array arr_str is expanded to hold seven elements; the last element contains a null character. Now, the character array arr_str is considered a character string because of the null character that is appended to the array.
You can also initialize a character array with a string constant. For example, the following statement initializes a character array, str, with a string constant, "Hello!":
char str[7] = "Hello!";
The compiler can automatically append a null character (\0) to the end of the array, and treat the character array as a character string. Note that the size of the array is specified to hold up to seven elements, although the string constant has only six characters enclosed in double quotes. The extra space is reserved for the null character that the compiler will add later.
You can declare an unsized character array if you want the compiler to calculate the total number of elements in the array. For instance, the following statement
char str[] = "I like C.";
initializes an unsized character array, str, with a string constant. Later, when the compiler sees the statement, it will figure out the total memory space needed to hold the string constant plus an extra null character added by the compiler itself.
If you like, you can also declare a char pointer and then initialize the pointer with a string constant. The following statement is an example:
char *ptr_str = "I teach myself C.";
WARNING
Don't specify the size of a character array as too small. Otherwise, it cannot hold a string constant plus an extra null character. For instance, the following declaration is considered illegal:
char str[4] = "text";
Note that many C compilers will not issue a warning or an error message on this incorrect declaration. The runtime errors that could eventually arise as a result could be very difficult to debug. Therefore, it's your responsibility to make sure you specify enough space for a string.
The following statement is a correct one, because it specifies the size of the character array str that is big enough to hold the string constant plus an extra null character:
char str[5] = "text";
String Constants Versus Character Constants
As you know, a string constant is a series of characters enclosed in double quotes (" "). On the other hand, a character constant is a character enclosed in single quotes (` `).
When a character variable ch and a character array str are initialized with the same character,
x, such as the following,
char ch = `x';
char str[] = "x";
1 byte is reserved for the character variable ch, and two bytes are allocated for the character array str. The reason that an extra byte is needed for str is that the compiler has to append a null character to the array.
Another important thing is that a string is interpreted as a char pointer. Therefore, you can assign a character string to a pointer variable directly, like this:
char *ptr_str;
ptr_str = "A character string.";
However, you can not assign a character constant to the pointer variable, as shown in the following:
ptr_str = `x'; /* It's wrong. */
In other words, the character constant `x' contains a right value, and the pointer variable ptr_str expects a left value. But C requires the same kind of values on both sides of an assignment operator =.
It's legal to assign a character constant to a dereferenced char pointer like this:
char *ptr_str;
*ptr_str = `x';
Now the values on both sides of the = operator are of the same type.
The program in Listing 13.1 demonstrates how to initialize, or assign character arrays with string constants.
TYPE
Listing 13.1. Initializing strings.1: /* 13L01.c: Initializing strings */
2: #include <stdio.h>
3:
4: main()
5: {
6: char str1[] = {`A', ` `,
7: `s', `t', `r', `i', `n', `g', ` `,
8: `c', `o', `n', `s', `t', `a', `n', `t', `\0'};
9: char str2[] = "Another string constant";
10: char *ptr_str;
11: int i;
12:
13: /* print out str2 */
14: for (i=0; str1[i]; i++)
15: printf("%c", str1[i]);
16: printf("\n");
17: /* print out str2 */
18: for (i=0; str2[i]; i++)
19: printf("%c", str2[i]);
20: printf("\n");
21: /* assign a string to a pointer */
22: ptr_str = "Assign a string to a pointer.";
23: for (i=0; *ptr_str; i++)
24: printf("%c", *ptr_str++);
25: return 0;
26: }
OUTPUT
The following output is displayed on the screen after the executable 13L01.exe of the program in Listing 13.1 is created and run from a DOS prompt:
ANALYSIS
C:\app>13L01
A string constant
Another string constant
Assign a string to a pointer.
C:\app>
As you can see from Listing 13.1, there are two character arrays, str1 and str2, that are declared and initialized in lines 6_9. In the declaration of str1, a set of character constants, including a null character, is used to initialize the array. For str2, a string constant is assigned to the array in line 9. The compiler will append a null character to str2 later. Note that both str1 and str2 are declared as unsized arrays for which the compiler can automatically figure out how much memory is needed. The statement in line 10 declares a char pointer variable, ptr_str.
The for loop in lines 14 and 15 then prints out all the elements in str1. Because the last element contains a null character (\0) that is evaluated as FALSE, the str1[i] expression is used in the second field of the for statement. The str1[i] expression returns logical TRUE for each element in str1 except the last one holding the null character. After the execution of the for loop, the string A string constant is shown on the screen.
Likewise, another for loop in lines 18 and 19 displays the string constant assigned to str2 by putting every element of the array on the screen. Because the compiler appends a null character to the array, the str2[i] expression is evaluated in the for statement. The for loop stops iterating when str2[i] returns a logical FALSE. By that time, the content of the string constant, Another string constant, has already been displayed on the screen.
The statement in line 22 assigns a string constant, "Assign a string to a pointer.", to the char pointer variable ptr_str. Also, a for loop is used to print out the string constant by putting every item in the string on the screen (see lines 23 and 24). Note that the dereferenced pointer *ptr_str is used to refer to one of the characters in the string constant. When the null character appended to the string is encountered, *ptr_str returns logical FALSE, which causes the iteration of the for loop to stop. In line 24, the *ptr_str++ expression indicates that the dereferenced pointer moves to the next character of the string after the current character referred to by the pointer is fetched. In Hour 16, "Applying Pointers," you'll learn more about pointer arithmetic.
- 16 views