How to Create a String of Characters in C++

By Stephen R. Davis

The elements of an array in C++ can be of any type. Arrays of floats, doubles, and longs are all possible; however, arrays of characters have particular significance.

In many cases, all values for each element are possible. However, C++ reserves the special “character” 0 as the non-character. You can use ‘’ to mark the end of a character array. (The numeric value of ‘’ is 0, but the type of ‘’ is char.)

The character ‘y’ is the character whose octal value is y. The character ‘’ is the character with a value of 0, otherwise known as the null character.

// DisplayString - output a character array to
//                 standard output, the MS-DOS window
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
// prototype declarations
void displayString(char stringArray[]);
int main(int nNumberofArgs, char* pszArgs[])
{
    char charMyName[] =
              {'S', 't', 'e', 'p', 'h', 'e', 'n', ''};
    displayString(charMyName);
    cout << endl;
    // wait until user is ready before terminating program
    // to allow the user to see the program results
    cout << "Press Enter to continue..." << endl;
    cin.ignore(10, 'n');
    cin.get();
    return 0;
}
// displayString - display a character string
//                 one character at a time
void displayString(char stringArray[])
{
    for(int i = 0; stringArray[i] != ''; i++)
    {
        cout << stringArray[i];
    }
}

The declaration of charMyName declares the character array with the extra null character ‘’ on the end. The displayString program iterates through the character array until a null character is encountered.

The function displayString() is simpler to use than its displayCharArray() predecessor because it is no longer necessary to pass along the length of the character array. This secret handshake of terminating a character array with a null is so convenient that it is used throughout the C++ language. C++ even gives such an array a special name.

A string of characters is a null-terminated character array. It is officially known as a null-terminated byte string, or NTBS. The simpler term C-string is also used to differentiate from the C++ type string.

The choice of ‘’ as the terminating character was not random. Remember that 0 is the only numeric value that converts to false; all other values translate to true. This means that the for loop could be (and usually is) written as

for(int i = 0; stringArray[i]; i++)

This whole business of null-terminated character strings is so ingrained in the C++ language psyche that C++ uses a string of characters surrounded by double quotes to be an array of characters automatically terminated with a ‘’ character. The following are identical declarations:

char szMyName[] = "Stephen";
char szAlsoMyName[] = 
              {'S', 't', 'e', 'p', 'h', 'e', 'n', ''};

The naming convention used here is exactly that, a convention. C++ does not care. The prefix sz stands for zero-terminated string.

The string Stephen is eight characters long and not seven — the null character after the n is assumed. The string is one character long, consisting of just the null character.