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?

4
  • 7
    No, std::string is a class. char x[] = {'H', 'i'}; is not a valid c-string, because there's no terminating '\0' character. Commented Oct 7, 2018 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)." Commented Oct 7, 2018 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. Commented Oct 7, 2018 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++? Commented Oct 7, 2018 at 22:01

4 Answers 4

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).

Sign up to request clarification or add additional context in comments.

9 Comments

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.
so is a C++ string always an 'array of characters'? or is it not necessarily?
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 stackoverflow.com/questions/33124479/….
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 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.
|
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.

1 Comment

"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.
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.

Comments

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.

1 Comment

contiguous memory from C++ 11.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.