A word about strings and pointers in C++

If you’re inexperienced with traditional C-style null terminated strings, the std::string.c_str() method may yield puzzling results.

This question just came up on Stack Overflow. It reflects a pretty common misunderstanding of how C-style strings are represented by char pointers in both C and C++.

Greatly condensed, it goes:

  • You read some data into a std:string object. You display the contents; it’s all there.
  • You invoke c_str() on that std:string, and display its contents; it’s not all there.

Continue reading “A word about strings and pointers in C++”