Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help
next up previous
Next: C++ Strings Up: C++ Summary Previous: Arrays

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:


Hello
My name is Robert
Rob
© Cambridge University Engineering Dept
Information provided by Tim Love
2006-12-08