// you’re reading...

C++ Glossary

Strings

A string

  • Is a series of characters stored in consecutive bytes of memory



C++ has two ways of dealing with strings

  • The first, taken from C and often called a C-style string, is the first one this page examines
  • Later, we will discuss an alternative method based on a string class library



The idea of a series of characters stored in consecutive bytes

  • Implies that you can store a string in an array of char
    • With each character kept in its own array element



Strings provide a convenient way to store text information

  • Such as messages to the user (“Please tell me your secret Swiss bank account number”)
    •  Or responses from the user (“You must be joking”)



C-style strings have a special feature:

  • The last character of every string is the null character
    • This character, written \0, is the character with ASCII code 0, and it serves to mark the string’s end
      • For example, consider the following two declarations (A-1):



Example of (A-1)

char dog [5] = { 'h', 'u', 's', 'k', 'y'}; // not a string!
char cat[5] = {'f', 'a', 't', 's', '\0'}; // a string!



(A-1) both of these arrays are arrays of char

  • But only the second is a string



The null character plays a fundamental role in C-style strings

  • For example, C++ has many functions that handle strings, including those used by cout
    • They all work by processing a string character-by-character until they reach the null character
      • If you ask cout to display a nice string like cat in the preceding example
        • It displays the first four characters, detects the null character, and stops
      • But if you are ungracious enough to tell cout to display the dog array from the preceding example, which is not a string
        • cout prints the five letters in the array and then keeps marching through memory byte-by-byte, interpreting each byte as a character to print, until it reached a null character
    • Because null characters, which really are bytes set to zero
      • Tend to be common in memory, the damage is usually contained quickly; nonetheless, you should not treat nonstring character arrays as strings



The cat array example makes initializing an array to a string look tedious

  • All those single quotes and then having to remember the null character
    • Don’t worry. There is a better way to initialize a character array to a string
      • Just use a quoted string, called a string constant or string literal, as in the following (A-2):



Example of (A-2)

char bird[10] = "Mr. Cheeps"; // the \0 is understood
char fish[] = "Bubbles"; // let the compiler count



Quoted strings always include the terminating null character implicitly

  • So you don’t have to spell it out
    • See (Image-1)



(Image-1) Initializing an array to a string
 

Initializing an array to a string



Also, the various C++ input facilities for reading a string from keyboard input into a char array

  • Automatically add the terminating null character for you



Of course, you should make sure the array

  • Is large enough to hold all the characters of the string
    • Including the null character



Initializing a character array with a string constant

  • Is one case where it may be safer
    • To let the compiler count the number of elements for you

 


There is no harm, other than wasted space, in making an array larger than the string

  • That’s because functions that work with strings are guided by the location of the null character
    • Not by the size of the array



C++ imposes

  • No limits on the length of a string



Remember

  • When determining the minimum array size necessary to hold a string
    • Remember to include the terminating null character in your count



Note that a string constant (with double quotes)

  • Is not interchangeable with a character constant (with single quotes)



A character constant

  • Such as ‘S’, is a shorthand notation for the code for a character
    • On an ASCII system, ‘S’ is just another way of writing 83
      • Thus, the following statement assigns the value 83 to shirt_size (A-3):



Example of (A-3)

char shirt_size = 'S'; // this is fine



But “S” represents the string consisting of two characters

  • The S and the \0 characters
    • Even worse, “S” actually represents the memory address at which the string is stored
      • So the following statement attempts to assign a memory address to shirt_size (A-4):



Example of (A-4)

char shirt_size = "S"; // illegal type mismatch



Because an address is a separate type in C++

  • A C++ compiler won’t allow this sort of nonsense
    • (We’ll return to this point later, after we’ve discussed pointers.)


Discussion

No comments for “Strings”

Post a comment