The difference between C-strings and std::string in C++ is ownership and safety. A C-string is a plain array of char that is ended by a null character, and it has no idea of its own length. A std::string owns its characters, it knows how long it is, and it grows when you add to it. The C-string form is how C did it, and std::string is how C++ does it.
The previous lesson in this module covered "Why Prefer std::array and std::vector Over Raw Arrays?", and the next lesson in this module is about "Which std::string Methods Handle Everyday Text?".
What a C-string actually is
A C-string is a null-terminated array of char. The terminator is a zero byte, and every function that works with C-strings relies on it to know where the text ends.
#include <iostream>
int main() {
const char title[] = "Bread"; // six chars: five letters plus the terminator
std::cout << "title: " << title << '\n';
std::cout << "bytes: " << sizeof(title) << '\n';
for (int i = 0; title[i] != '\0'; ++i) {
std::cout << "title[" << i << "] = '" << title[i] << "'\n";
}
return 0;
}The output shows the terminator doing its job.
title: Bread
bytes: 6
title[0] = 'B'
title[1] = 'r'
title[2] = 'e'
title[3] = 'a'
title[4] = 'd'Notice that sizeof(title) is 6 even though "Bread" has five letters. The array includes the terminating zero.
Why C-strings are dangerous
The danger is that nothing enforces the terminator. If a function writes past the end of the buffer, or reads a buffer that was not terminated, then the result is undefined behaviour.
#include <cstring>
#include <iostream>
int main() {
char buffer[8];
std::strcpy(buffer, "Short"); // fits: 5 chars plus the terminator
std::cout << "buffer: " << buffer << '\n';
std::cout << "length: " << std::strlen(buffer) << '\n';
// buffer now holds "Short" and three spare bytes.
// Writing a longer string here would run off the end.
return 0;
}The output is what you would expect.
buffer: Short
length: 5But the same code with a longer string writes past the end of the array, and there is no compiler check for it. This is the single biggest reason that std::string replaced C-strings in new C++ code.
std::string owns its data
std::string stores a length, a pointer and its characters, and it manages the memory for you.
#include <iostream>
#include <string>
int main() {
std::string title = "Bread";
title += " and Butter";
title += '!';
std::cout << "title: " << title << '\n';
std::cout << "length: " << title.length() << '\n';
std::cout << "empty: " << std::boolalpha << title.empty() << '\n';
return 0;
}The output shows the string growing without any buffer to size by hand.
title: Bread and Butter!
length: 17
empty: falseThere is no terminator to think about, there is no capacity to manage, and the object copies itself correctly when you assign it.
Converting between the two forms
Going from a C-string to a std::string is automatic, because the constructor takes a const char*. Going the other way uses .c_str(), which returns a pointer to the internal null-terminated buffer.
#include <cstring>
#include <iostream>
#include <string>
int main() {
std::string shelf = "Fiction";
const char* raw = shelf.c_str();
std::cout << "raw: " << raw << '\n';
std::cout << "length: " << std::strlen(raw) << '\n';
std::string copy(raw);
std::cout << "copy: " << copy << '\n';
return 0;
}The output shows the round trip.
raw: Fiction
length: 7
copy: FictionThere is one warning about .c_str(). The pointer that it returns is valid only for as long as the string is not modified or destroyed. If you keep a pointer to a string and then you change the string, the pointer may dangle.
Comparing C-strings correctly
Comparing two C-strings is done with std::strcmp, and not with the equals sign. The equals sign on two const char* compares the addresses and not the text that they point at.
#include <cstring>
#include <iostream>
int main() {
char first[] = "shelf";
char second[] = "shelf";
std::cout << "strcmp: " << std::strcmp(first, second) << '\n';
std::cout << "content eq: " << (std::strcmp(first, second) == 0) << '\n';
std::cout << "pointer eq: " << (&first[0] == &second[0]) << '\n';
return 0;
}The output shows the difference clearly.
strcmp: 0
content eq: 1
pointer eq: 0The two arrays hold identical text, so std::strcmp says that they are equal, but they are different objects at different addresses, so the pointer comparison is false. The same applies to a C-string and a std::string, because comparing them with the equals sign does not compare their contents.
When a raw char array is still right
A C-string is not always wrong. You use a raw char array when you are talking to a C API that expects one, when you are handling a fixed block of bytes such as a binary format, or when the data genuinely is an array of characters rather than a piece of text, such as a byte buffer.
#include <cstring>
#include <iostream>
void describe_buffer(const char* data, std::size_t length) {
std::cout << "length: " << length << '\n';
std::cout << "text: ";
for (std::size_t i = 0; i < length; ++i) {
std::cout << data[i];
}
std::cout << '\n';
}
int main() {
char raw[16] = {0};
std::strncpy(raw, "Vintage", sizeof(raw) - 1);
describe_buffer(raw, std::strlen(raw));
return 0;
}This is the shape that you need for any C library function that takes a buffer and a length. The output confirms that both worked.
length: 7
text: VintageQuick reference
- A C-string is a char array that ends in the null character, and a
std::stringowns its characters. sizeofon a C-string includes the terminator, so "Bread" is six bytes.std::strlenwalks the string looking for the terminator, so an unterminated buffer reads out of bounds.std::stringknows its length, it grows on demand, and it copies safely.- Constructing a
std::stringfrom aconst char*is automatic, and.c_str()goes the other way. - A C-string needs a fixed buffer, and a
std::stringgrows on demand.
Try it yourself: write a program that reads a line of text into a std::string and then copies it into a fixed char[32] buffer using std::strncpy, printing the length of each. Then try a title that is longer than 32 characters and see how the two approaches differ. Finally, replace the raw buffer with a std::string and confirm that the program no longer has to think about the buffer size at all.