9

How do we differentiate char arrays and string in c++? Is there anything char arrays do better than std::string ?

7
  • 1
    @starboy_jb Standard algorithms work equally well with char arrays. Commented Dec 31, 2020 at 18:19
  • 5
    Come on, this does not have to be opinion-based. E.g. std::string cannot be used in constexpr context before C++20, so I still prefer const char[] for global constants unless I need to do some string operations with them. Commented Dec 31, 2020 at 18:20
  • 5
    A typical use case is when you want to reuse old code/library, that you wrote decades ago, and don't want to rewrite! Commented Dec 31, 2020 at 18:22
  • 6
    @Quimby constexpr string_views should also work. Voted to reopen. Commented Dec 31, 2020 at 18:22
  • 2
    @Quimby You should write it as an answer Commented Dec 31, 2020 at 18:35

2 Answers 2

5

How do we differentiate char arrays and string in c++?

You don't, string literals are by definition null-terminated char arrays. Since arrays decay into pointers the first chance they get, const char* is (still) often a synonym for string.

If you are asking about when you should write new char[n], the answer is never. If anything, it should be std::make_unique<char[]>(n); and unless you are writing your own version of std::string, use the standard one. If you need a buffer, use std::vector or std::array.

There are some advantages of const char[] constants over const std::string but they are being "solved" by the new C++ Standards:

  1. Before C++20, std::string could not be used in constexpr context. So, I still prefer declaring global string constants with constexpr const char[] if all I do is just passing them to some function. As @HolyBlackCat mentioned in the comments, C++17 std::string_view makes this use-case obsolote too, especially with the new sv literal:

    #include <string_view>
    using namespace std::literals;
    //Compile-time string_view
    constexpr auto str = "hello"sv;
    
  2. const char* is somewhat more universal. You can pass it to a function accepting const char*, std::string, or std::string_view. The reverse requires std::string::c_str() and it is not possible to so without copying the std::string_view.

  3. There is no dynamic allocation involved. Although std::string might employ SSO, it is not guaranteed. This might be relevant for very small systems where the heap is precious and the program flash memory is more accomodating and contains the literal anyway.

  4. Interacting with old libraries. But even then, std::string is null-terminated too.

Overall, my recommendation would be to use std::string_view every chance you get - for any non-owning string, including holding string literals. Most importantly, it should replace const char* and const std::string& function parameters. If you want to own a string, use std::string.

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

2 Comments

This suggested use of string_view has a problem: if you then need to call a function that expects a read-only null-terminated string (e.g. third-party library), you have to copy the whole viewed string to add the terminator, even though the source for the string-view already had it. Which is a waste of time and resources.
@M.M Yes, that is correct, my advice was aimed at new code without old dependencies. It's a tradeoff, std::string_view can do non-suffix-based substrings without copy, which ordinary read-only null-terminated string cannot.
4

One reason (which I personally don't think is a very good one) to use char arrays is the use case when you want your code to compile with both a C compiler and a C++ compiler.

2 Comments

@bruno I was debating about that with myself, but I think it is a valid answer. However rare and misused this use case may be, it is a use case. And it is something that char arrays can do and std::string cannot, which is exactly what OP asked for.
if the goal is to also compile in C there is no choice so no question too ;-)

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.