1

So I know that all C-strings are arrays of characters in C++. What about strings?

Also why is char x[] = {'H', 'i'}; NOT a C-string? x is an array of characters which means it's a C-string. Am I missing something?

ShadowMitia
  • 2,411
  • 1
  • 19
  • 24
NewLearner
  • 153
  • 11
  • 7
    No, `std::string` is a class. `char x[] = {'H', 'i'};` is not a valid c-string, because there's no terminating `'\0'` character. – πάντα ῥεῖ Oct 07 '18 at 20:42
  • The description of the `c-strings` tag you used: "*A string in the programming language C is represented as a sequence of characters followed by a null terminator (represented as \0).*" – melpomene Oct 07 '18 at 20:48
  • Are you asking specifically about `std::string`? Because in `C++` you could choose to represent *string type objects* in all sorts of ways. – Galik Oct 07 '18 at 21:52
  • Thanks for all the answers. But I wanna be clear on this question: Is a string(not C-string) an array of characters in C++? – NewLearner Oct 07 '18 at 22:01

4 Answers4

3

I just found good explanation for your question here: C strings and C++ strings. In short:

A C string is usually declared as an array of char. However, an array of char is NOT by itself a C string. A valid C string requires the presence of a terminating "null character" (a character with ASCII value 0, usually represented by the character literal '\0').

A C++ string is an object of the class string, which is defined in the header file and which is in the standard namespace. The string class has several constructors that may be called (explicitly or implicitly) to create a string object.

Representations in memory:

C-String C-String

C++ string C++ string

Updated:

You should understand that:

  1. C++ offers more character types than char in C, e.g. UTF-16 and UTF-32.

  2. C++ defines a thing called std::basic_string that is a class template for making strings out of those character types.

  3. C++ typedef the class template for char type as std::string.

Now, you know that std::string is the basic_string for char-typed characters. Referring to Does std::string need to store its character in a contiguous piece of memory?, if you are mentioning std::string then for C++ 11 and later versions, it is essentially AN array (not TWO or MORE arrays) of char-typed characters. For C++ versions prior to C++ 11 or for some other types of characters (I am not sure actually), the underlying memory might not be contiguous (i.e. it may need TWO or MORE arrays (not AN array) for storing a string).

duong_dajgja
  • 4,196
  • 1
  • 38
  • 65
  • The C++ string representation is not true pre C++ 11 because they weren't guaranteed to be laid out contiguously in memory and for C++ 11 and on, they are guaranteed to be nul terminated. – George Oct 07 '18 at 21:05
  • so is a C++ string always an 'array of characters'? or is it not necessarily? – NewLearner Oct 07 '18 at 21:25
  • Since "The C++ string representation is not true pre C++ 11 because they weren't guaranteed to be laid out contiguously in memory" then it could be composed of multiple arrays of characters (not necessarily AN array) -- this thing actually varies depending C++ versions and the character type. See more at https://stackoverflow.com/questions/33124479/does-stdstring-need-to-store-its-character-in-a-contiguous-piece-of-memory. – duong_dajgja Oct 07 '18 at 21:35
  • so... I read the answers on that link... couldn't really understand much...because I'm a beginner... but it seems like you're saying that in 'some C++ versions', all strings are 'arrays of characters' while in 'some other C++ versions', strings are NOT arrays of characters. correct? – NewLearner Oct 07 '18 at 21:56
  • @NewLearner Since C++11, the standard guarantees that the pointer returned by the `data()` member of `std::string` points to `size()` characters followed by a `'\0'`. Previously there wasn't such a member, and the member `c_str()` *might* return a copy. – Caleth Oct 07 '18 at 22:04
  • @Caleth so since C++11, ALL strings(not c-strings) are arrays of characters? – NewLearner Oct 07 '18 at 22:05
  • @NewLearner what do you mean by *are*? There is a required to be something sufficiently `char[]` like *somewhere*, *owned* by the `std::string` instance. Many implementations locate that *within* the `std::string` instance when the size is sufficiently small, but in general it is supplied by a `std::allocator` from the free store – Caleth Oct 07 '18 at 22:13
  • @NewLearner I added more explanations. – duong_dajgja Oct 07 '18 at 22:22
  • @duong_dajgja Thank you so much. Just to clarify, since C++ 11 and later, a string type object and a c-string type object are BOTH an array of characters but only the c-string type object has '\0' at the end right? – NewLearner Oct 07 '18 at 23:11
0

C string is a null terminated string, so, any character array that doesn't end in zero, is not a C string.

C string is character array char * or char []

But about the C++ strings, it is a class that has a lot of operations (methods) that can happen on it, it has length and it checks the length (like C++ vector and array) when you try to access an array subscript of it (using the at method). Its copying may be more performant because length to allocate a new memory location is known.

You can create a std::string from a C string. There is a constructor that takes a C string and a length and create a std::string from them.

All C functions that handles strings, treat C strings as null terminated, so if it is not, C would keep searching for the nearest next null, which is extremely dangerous.

user9335240
  • 1,739
  • 1
  • 7
  • 14
  • 3
    "*it checks the length (like C++ `vector` and `array`) when you try to access an array subscribe of it*" - not true. Only `.at()` checks for a valid index. – melpomene Oct 07 '18 at 20:52
0

C++ offers standard library form in which class string is defined. Class String is not just a C character string (char* C or char sz[]). Class string has methods and attributes while the latter no. Also class string its size changes runtime while the latter not. It also manage memory for you in its Ctor and dtor. As an advice use Class string as much as possible rather than using char*.

  • Class String appends a NULL Character \0 at the end of the string as a sign of the end of string while the latter no.
Raindrop7
  • 3,889
  • 3
  • 16
  • 27
0

As mentioned in the previous answers char x[] = {'H', 'i'}; is not a valid C String as there is no null character to identify the termination or end of string. C++ Standard Template provides class String which guarantees dynamic contiguous memory allocation (similar to Vector) and easy access by index. more on this can be found in https://en.cppreference.com/w/cpp/string/basic_string.

Jolly Jose
  • 84
  • 5