String termination – char c=0 vs char c=’\0′

http://en.wikipedia.org/wiki/Ascii#ASCII_control_code_chart Binary Oct Dec Hex Abbr Unicode Control char C Escape code Name 0000000 000 0 00 NUL ␀ ^@ \0 Null character There’s no difference, but the more idiomatic one is ‘\0’. Putting it down as char c = 0; could mean that you intend to use it as a number (e.g. a counter). … Read more

Can a std::string contain embedded nulls?

Yes you can have embedded nulls in your std::string. Example: std::string s; s.push_back(‘\0’); s.push_back(‘a’); assert(s.length() == 2); Note: std::string‘s c_str() member will always append a null character to the returned char buffer; However, std::string‘s data() member may or may not append a null character to the returned char buffer. Be careful of operator+= One thing … Read more

What are null-terminated strings?

What are null-terminating strings? In C, a “null-terminated string” is a tautology. A string is, by definition, a contiguous null-terminated sequence of characters (an array, or a part of an array). Other languages may address strings differently. I am only discussing C strings. How are they different from a non-null-terminated strings? There are no non-null-terminated … Read more

What is a null-terminated string?

A “string” is really just an array of chars; a null-terminated string is one where a null character ‘\0’ marks the end of the string (not necessarily the end of the array). All strings in code (delimited by double quotes “”) are automatically null-terminated by the compiler. So for example, “hi” is the same as … Read more