Cstring Values and Cstring Variables

  1. A cstring variable is a partially filled array of characters with the null character '\0' automatically placed when the array is created as the last element of the array to mark the end of the cstring.

  2. It is called a cstring because of its C heritage.

  3. We have encountered cstrings before. For example in a statement

    cout << "Enter the input:";

    "Enter the input:" is a cstring literal.

  4. A cstring variable named aCstring capable of storing a string having up to (maxSize - 1) characters is declared as follows:

    char aCstring[maxSize];

    For example the following declares a cstring variable s capable of storing 9 or fewer characters:

    char s[10];

  5. A cstring variable can be initialized when it is declared. For example:

    char s[10] = "Hi Mom!";

    declares and initializes a partially filled array with elements as shown below:

    s[0] s[1] s[2] s[3] s[4] s[5] s[6] s[7] s[8] s[9]
    H i M o m ! \0 ? ?
    Note that the null character '\0' is automatically placed as the last element of the array to mark the end of the cstring.

  6. When initializing a cstring variable, the array size can be omitted. C++ will automatically make the size of the cstring variable one more than the number of input characters in order to accommodate all the characters and the ending null character '\0'.

    For example, the initialization

    char short_string[ ] = "abc";

    is equivalent to

    char short_string[4] = "abc";

    However it is not equivalent to

    char short_string[ ] = {'a', 'b', 'c'};

    which declares an array containing only the 3 characters, 'a', 'b',and 'c', but not the ending null character. The array is a char array but is not a cstring.

  7. A cstring variable is an array and so it has indexed variables (elements) that can be used like those of any other array. For example, by the declaration

    char dirtyString[7] = "Dirty";

    one has 7 indexed variables to work with. For example the following code will clean up dirtyString by replacing each of its characters with an 'x', except the ending null character.

    for (int n = 0; dirtyString[n] != '\0'; n++)
       dirtyString[n] = 'x';

  8. Care must be exercised to ensure that the null character is not lost when manipulating these indexed variables. If the null character is lost, the resulting array is simply an array of characters but is no longer a cstring.

  9. Many cstring manipulating functions rely on the detection of the null character for them to work properly. In the above example, if dirtyString has for some reason lost its null character, the for-loop will never stop. A large amount of data beyond the end of the array will be dangerously altered.

  10. A safer way to implement the above code is to store the maximum declared size of the dirtyString in a constant, say SIZE, and rewrite it as:

    for (int n = 0; (dirtyString[n] != '\0') && (n < SIZE); n++)
       dirtyString[n] = 'x';