8.1. Introduction To Strings And C-Strings

Time: 00:03:48 | Download: Large, Large (CC), Small | Streaming Streaming (CC) | Slides (PDF)

Students transitioning from Java may find it odd that C++ has two different representations for string data. Historically, strings were a primitive or fundamental data type based on character arrays and pointers. C++ inherits these fundamental strings, now called C-strings, from the C programming language. (I've read two explanations for the name C-string: (1) they are strings of characters, and (2) they are the kind of strings used in the C programming language - take your pick.) Furthermore, the first releases of C++ did not include a native string class (and creating a string class based on character arrays was, and still is, a common example used to introduce many object-programming concepts).

Today, C++ does include a full-featured string class similar to Java's String class (but take notice that in C++ the class name begins with a lower-case letter). Nevertheless, C-strings are still important for many kinds of C++ programs, so the chapter covers both strings. The natural progression of topics begins with arrays (covered in the previous chapter), then C-strings (a special use of character arrays), and ends with the string class.

A depiction of a string as an array or series of boxes. The string holds some characters indicated as used or filled array elements. Some array elements are empty or unused, indicated as empty boxes. The string's capacity is the sum of the filled and empty array elements.
Fundamental string characteristics. In general, strings (C-strings and string objects) have three fundamental characteristics: