Declaring and initializing a string
There are several ways to declare and initialize a string. We will explore the various ways to both declare and initialize strings in this section.
String declarations
We can declare a string in several ways. The first way is to declare a character array with a specified size, as follows:
char aString[8];
This creates an array of 8
elements, capable of holding seven characters (don't forget the terminating NUL
character).
The next way to declare a string is similar to the first method but instead, we don't specify the size of the array, as follows:
char anotherString[];
This method is not useful unless we initialize anotherString
, which we will see in the next section. As you may recall from Chapter 14, Understanding Arrays and Pointers, this declaration looks like a pointer in the form of an array declaration. In fact, without initialization, it is.
The last...