How does is differ from a usual string?
A null-terminated string is a contiguous sequence of characters, the last one of which has the binary bit pattern all zeros. I'm not sure what you mean by a "usual string", but if you mean std::string, then a std::string is not required (strictly) to be contiguous, and is not required to have a terminator. Also, a std::string's string data is always allocated and managed by the std::string object that contains it; for a null-terminated string, there is no such container, and you typically refer to and manage such strings using bare pointers.
All of this should really be covered in any decent C++ text book - I recommend getting hold of Accelerated C++, one of the best of them.
A null-terminated string means that the end of your string is defined through the occurrence of a null-char (all bits are zero).
"Other strings" e.g. have to store their own lenght.
A "string" is really just an array of char
s; 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 {'h', 'i', '\0'}
.
A null-terminated string is a native string format in C. String literals, for example, are implemented as null-terminated. As a result, a whole lot of code (C run-time library to begin with) assumes that strings are null-terminated.
There are two main ways to represent a string:
1) A sequence of characters with an ASCII null (nul) character, 0, at the end. You can tell how long it is by searching for the terminator. This is called a null-terminated string, or sometimes nul-terminated.
2) A sequence of characters, plus a separate field (either an integer length, or a pointer to the end of the string), to tell you how long it is.
I'm not sure about "usual string", but what quite often happens is that when talking about a particular language, the word "string" is used to mean the standard representation for that language. So in Java, java.lang.String is a type 2 string, so that's what "string" means. In C, "string" probably means a type 1 string. The standard is quite verbose in order to be precise, but people always want to leave out what's "obvious".
In C++, unfortunately, both types are standard. std::string is a type 2 string[*], but standard library functions inherited from C operate on type 1 strings.
[*] Actually, std::string is often implemented as an array of characters, with a separate length field and a nul terminator. That's so that the c_str()
function can be implemented without ever needing to copy or re-allocate the string data. I can't remember off-hand whether it's legal to implement std::string without storing a length field: the question is what complexity guarantees are required by the standard. For containers in general size()
is recommended to be O(1), but isn't actually required to be. So even if it is legal, an implementation of std::string that just uses nul-terminators would be surprising.