char Arrays

A string is a series of characters. The only strings you've seen until now have been unnamed string constants used in cout statements, such as

cout << "hello world.\n";

In C++, a string is an array of chars ending with a null character. You can declare and initialize a string just as you would any other array. For example:

char Greeting[] = { 'H', 'e', 'l', 'l', 'o', ' ',
'W','o','r','l','d', '\0' };

The last character, '\0', is the null character, which many C++ functions recognize as the terminator for a string. Although this character-by-character approach works, it is difficult to type and admits too many opportunities for ...

Get Sams Teach Yourself C++ in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.