Notes on Section 8.1


  • Null character

  • C string


  • char str_var[20] = "Kenyon Rules!" creates a character array of size 20, just as you'd expect. The only difference is that after str_var[12] gets the value '!', the value of '\0', the null character, is assigned to str_var[13].

  • There is a two-page table of commands found in the cstring library on page 456 of your textbook. I won't repeat the table here, but it's all new syntax. You do not need to memorize these commands.

  • cin.getline(str_var, N); gets up to N-1 characters of input from the keyboard and puts them into the variable str_var which should be a character array variable. Why only N-1? Because it also puts the null character at the end of whatever it reads to make sure that the character array is a C string. (This is a member function of all input streams, not just cin.)


  • C strings always end with the null character, which we'll write '\0' even though it's a single character. In fact, that is the one defining characteristic of a C string. It is a character array with a '\0' somewhere in it.

  • The '\0' in a C string indicates the end of the string. Everything in the array past that point is presumed to be garbage.

  • In all other respects, C strings behave exactly like regular character arrays.

  • Be very careful not to overwrite the '\0' when dealing with a C string as an array entry-by-entry. If you do, most string functions will get you into really bad long or infinite loops, since they run until they see the '\0' and will happily overwrite millions of bytes of memory thinking that one is just around the corner.

  • The following commands accomplish the same thing:

char str_var[14] = "Kenyon Rules!";

char str_var[] = "Kenyon Rules!";

char str_var[] = {'K','e','n','y','o','n',' ',


However, if you leave out the '\0' in the last, it is not a C string, but just a

generic character array.

  • You may not use = for assignment or = = for testing equality of C strings. (You may use = to initialize, but not for later reassignments.) There are functions in the cstring library to do all of this and more.

  • Both strcpy and strcat are unsafe in that they don't make sure you don't overrun the end of you allowed space in a character array. There are safe versions of both of these functions which take an input for the maximum size of the array.

  • Remember that getline needs room for the '\0' character.

Notes on Section 8.2


  • #include<string>

  • string name;

  • string first = "Sean";

  • string last("Decatur");

  • name = first + " M. " + last;

  • getline(cin, line); is the string class version of the C-string cin.getline command.

  • getline(cin, line, '!') reads input until it sees the character ! , putting it into the string variable line.

  • The array portion of a variable of type string may be accessed directly with name[3].

  • The same character variable may be accessed with the safer version for both reading and writing purposes. The "safer" means that if you use an out-of-range index, you will get a run-time warning/error message.

  • There are many functions from the string class defined on page 477. We will mention only a few here.

  • name.empty() returns the obvious Boolean value

  • Objects of type string may be compared using the usual comparison operators, including testing first = last and first last.

  • To convert objects of type string to plain old C-strings, use name.c_str().


  • Be aware that what's going on with the command name = "S. " + first + " " + last; is really quite complicated, since things like "S. " are not, in fact, string objects but C-strings. They first have to be converted using a constructor defined within the string class, and the compiler knows to do this because + has been overloaded for two string objects. It has not been overloaded for two C-strings, so you must have at least one explicitly string variable when you are using the + operator.

  • The >> operator is overloaded to work with string variables as it does with C-strings (as well as characters, integers, etc.), but as in all other contexts, it ignores any preliminary whitespace and treats whitespace after any other type of character as the end of its input. To get an input which includes spaces, returns, etc, you need to use the getline command.

  • It's an odd historical accident that cin.getline(one_line, 80), a member function of the istream class, takes an old-fashioned C-string variable (even though it looks modern and object-oriented); whereas the old-fashioned stand-alone function getline(cin, line) is designed to work with the modern string class. Go figure.

Topic revision: r1 - 2015-10-27 - JimSkon
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback