john
john

Reputation: 15

What's the necessity of string in c++ while we already have char[]?

Many topics have discussed the difference between string and char[]. However, they are not clear to me to understand why we need to bring string in c++? Any insight is welcome, thanks!

Upvotes: 1

Views: 91

Answers (3)

anatolyg
anatolyg

Reputation: 28239

As noted by MSalters in a comment, strings can grow. This is, in my opinion, the strongest reason to have them in C++.

For example, the following code has a bug which may cause it to crash, or worse, to appear to work correctly:

char message[] = "Hello";
strcat(message, "World");

The same idea with std::string behaves correctly:

std::string message{"Hello"};
message += "World";

Additional benefits of std::string:

  1. You can send it to functions by value, while char[] can only be sent by reference; this point looks rather insignificant, but it enables powerful code like std::vector<std::string> (a list of strings which you can add to)
  2. std::string stores its length, so any operation which needs the length is more efficient
  3. std::string works similarly to all other C++ containers (vector, etc) so if you are already familiar with containers, std::string is easy to use
  4. std::string has overloaded comparison operators, so it's easy to use with std::map, std::sort, etc.

Upvotes: 2

Lev M.
Lev M.

Reputation: 6269

char[] is C style. It is not object oriented, it forces you as the programmer to deal with implementation details (such as '\0' terminator) and rewrite standard code for handling strings every time over and over.

char[] is just an array of bytes, which can be used to store a string, but it is not a string in any meaningful way.

std::string is a class that properly represents a string and handles all string operations.
It lets you create objects and keep your code fully OOP (if that is what you want). More importantly, it takes care of memory management for you.

Consider this simple piece of code:

// extract to string
#include <iostream>
#include <string>

main ()
{
    std::string name;

    std::cout << "Please, enter your name: ";
    std::cin >> name;
    std::cout << "Hello, " << name << "!\n";

    return 0;
}

How would you write the same thing using char[]? Assume you can not know in advance how long the name would be!

Same goes for string concatenation and other operations. With real string represented as std::string you combine two strings with a simple += operator. One line.

If you are using char[] however, you need to do the following:

  1. Calculate the size of the combined string + terminator character.
  2. Allocate memory for the new combined string.
  3. Use strncpy to copy first string to new array.
  4. Use strncat to append second string to first string in new array.

Plus, you need to remember not to use the unsafe strcpy and strcat and to free the memory once you are done with the new string.

std::string saves you all that hassle and the many bugs you can introduce while writing it.

Upvotes: 2

cocool97
cocool97

Reputation: 1251

String class is no more than an amelioration of the char[] variable.
With strings you can achieve the same goals than the use of a char[] variable, but you won't have to matter about little tricks of char[] like pointers, segmentation faults...
This is a more convenient way to build strings, but you don't really see the "undergrounds" of the language, like how to implement concatenation or length functions...
Here is the documentation of the std::string class in C++ : C++ string documentation

Upvotes: 0

Related Questions