NewLearner
NewLearner

Reputation: 153

In C++, is a string type object an array of characters just like a C-string object?

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?

Upvotes: 1

Views: 2404

Answers (4)

duong_dajgja
duong_dajgja

Reputation: 4276

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

Upvotes: 3

Jolly Jose
Jolly Jose

Reputation: 84

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.

Upvotes: 0

Raindrop7
Raindrop7

Reputation: 3911

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.

Upvotes: 0

user9335240
user9335240

Reputation: 1789

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.

Upvotes: 0

Related Questions