0Pricing
C++ Academy · Lesson

std::string vs C-Style Char Arrays

Compare modern std::string with legacy null-terminated char arrays and decide when to use each.

Two Ways to Hold Text

C++ inherits C-style strings — null-terminated char arrays — and adds std::string from the standard library. Almost always use std::string.

C-Style Strings

A C-style string is a sequence of char ending with a null byte '\0'. Functions like strlen walk the array until they find the null.

const char* greeting = "Hello";
// Memory: H, e, l, l, o, \0  (6 bytes)

#include <cstring>
std::cout << std::strlen(greeting);  // 5

All lessons in this course

  1. std::string vs C-Style Char Arrays
  2. String Operations: concat substr find replace
  3. String Stream stringstream for Parsing
  4. String Conversions: to_string and stoi
← Back to C++ Academy