Department of Engineering

IT Services

Character arrays

A common use of the one-dimensional array structure is to create character strings. A character string is an array of type char which is terminated by the null terminator character, '\0'. String variables are manipulated using the usual array operations with the addition that string literals (a sequence of characters within double quotes) may be used to initialise char arrays and can be used in text output statements, e.g.:

char greeting[6] = "Hello";   // '\0' automatically appended
char name[] = "Robert";       // Length automatically set to 7

cout << greeting << endl;
cout << "My name is " << name << endl;
cout << name[0] << greeting[4] << name[2] << endl;

prints out:

My name is Robert