Can I use both #include<cstring> and #include<string> in the same cpp file? |
Yes.
A C string is not a data type, it's a data format. The header <cstring> contains convenience functions that manipulate C strings.
In your example above, arr is not a C string. It's array that
contains a C string. It could be overwritten to contain something else. For example:
1 2 3
|
char arr[] = "abc";
for (int i = 0; i < 4; i++)
arr[i] = i + 1;
|
Now arr no longer contains a C string, because a C string is supposed to mark its end with a null character, but arr ends before any null character can be found. If you pass arr to any C string manipulation function, such as strlen(), you'll cause undefined behavior, because the program will continue reading past the end of arr.
On the other hand, <string>
does define several data types, such as std::string, std::wstring, etc.
As for your other question, yes, an array declared as
char arr[] = "abc";
is guaranteed to be as long as the specified string, plus 1 more element to hold the null character. It's equivalent to
|
char arr[] = { 'a', 'b', 'c', 0 };
|