C++ PROGRAMMING ยท 12 MIN STUDY

C-style strings and character arrays

Understand null-terminated character arrays and how they differ from std::string.

Learning objectives

  • Explain what a C-style string is.
  • Understand the null terminator.
  • Distinguish a character array from a std::string.
  • Recognize common buffer and bounds risks.

Prerequisites: understand character types, arrays, indexing, and the idea of a terminating element. Review Arrays and Strings first if these are unfamiliar.

What is a C-style string?

A C-style string is a sequence of characters stored in a character array and terminated by a special null character, written '\0'. The terminator marks where the text ends. It is not the same as the character digit zero '0'.

char word[4] = {'c', 'a', 't', '\0'};

index:  0    1    2    3
value: 'c'  'a'  't'  '\0'

The array needs room for all visible characters plus the null terminator. A three-letter word therefore needs at least four char elements.

String literal initialization

char language[] = "C++";
// array contains 'C', '+', '+', '\0'

The compiler includes the terminating null character when initializing an array from a string literal. Do not write into a string literal through a pointer; modifying a string literal has undefined behavior.

Why std::string is usually safer

std::string manages its own storage and size. C-style strings rely on a terminator and careful buffer management. Functions that copy or concatenate raw character arrays can overflow a destination buffer if the programmer does not verify the available space. Prefer std::string for ordinary C++ text.

Common mistakes

  • Forgetting room for '\0'.
  • Confusing '\0' with '0'.
  • Reading beyond the end of a character array.
  • Assuming every char array is a valid null-terminated string.
  • Modifying a string literal.

Practice

  1. Draw the contents of char name[] = "Asha"; including the terminator.
  2. Explain why char word[3] = "cat"; cannot hold the null terminator.
  3. Use std::string to read and modify a line of text.
  4. Explain why raw buffer copying can be unsafe.

Practice solutions

Show worked solutions
  1. char name[] = "Asha"; creates five elements: 'A', 's', 'h', 'a', and the terminating '\0'.
  2. The literal "cat" contains three visible characters plus a terminating null character. A three-element array cannot hold all four elements, so the initialization is ill-formed in C++.
  3. Prefer std::string line; followed by std::getline(std::cin, line); modify it with string operations such as line += "!". Include <string> and <iostream>.
  4. Raw buffer copying can write past the destination if its capacity is too small, omit the terminator, or overlap incorrectly. Prefer std::string unless a C API requires a character buffer.

Check your understanding

How many array elements are needed for the C-style string "dog" including its terminator?

Answer: Four: three letters plus the null character.

Summary

C-style strings are null-terminated character arrays. Their storage and bounds must be managed carefully. Prefer std::string in normal C++ code unless an interface specifically requires a C-style string.

References