26A. Null-Terminated Strings

Characters

We have seen characters such as 'a', '0' and '$'. Each has an associated integer value called its code. For example, the ASCII alphabet assigns code 97 to 'a', code 48 to '0' and code 36 to '$'.

We will need a special character '\0', called the null character. Its character code is 0.

See functions on characters for useful tools for characters.

Null-terminated strings

A string can be represented as an array of characters. But remember that, in general, you cannot find out how large an array is by looking at the array, since an array is just a pointer to the first thing in a chunk of memory. To work around that, we put a null character at the end of the string. For example, an array s containing five characters

  s[0] = 'g'
  s[1] = 'o'
  s[2] = 'a'
  s[3] = 't'
  s[4] = '\0'
represents the string "goat" as a null-terminated strings. The null character is not part of the string, but is only a marker letting you know where the string ends.

You can pass a null-terminated string to a function without passing a separate size, because the function can find out how long the string is by looking for the null character.

String constants

A string constant such as "some text" is a null-terminated string. If string constant C is n characters long, then the compiler puts an array of n+1 bytes into the machine-language program, and stores C in that array, as a null-terminated string.

Null-terminates strings are arrays!

A null-terminated string is an array, and you cannot afford to forget that.

Strings are not automatically copied

Arrays are not automatically copied, so null-terminated strings are not automatically copied. Suppose str1 is a null-terminated string. Doing statement

  char* str2 = str1;
does not make str2 a copy of str1. Only the pointer is copied, as shown in the following diagram.

Some students try to get around that by writing

  char* str2 = *str1;
imagining that *str1 is the chunk of memory itself. But you already know about arrays. Expression *A is the same as A[0]. Would
  char* str2 = str1[0];
work? Of course not, since str1[0] has type char and str2 has type char*. You can do
  char str2 = str1[0];
but calling the variable str2 does not make it a string. Now, variable str2 is a variable of type char that holds the first character of string str1.

We will see how to copy a null-terminated string on the next page.

Exercises

  1. What do you need to do to ensure that a string constant is null-terminated? Answer

  2. If s is a null-terminated string, how can you refer to the first character in s. Answer