1

I'm new to c++ and I'd like to know right from the start,

Does any of these methods of making strings work exactly the same way and give the exact same results always in every case? is there any difference in the result in any of them?

1) char greeting [6] = { 'h','e','l','l','o','\0' };

2) char greeting[] = "hello";

3) #include <string>
   string greeting = "hello";
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Aaron
  • 113
  • 1
  • 8
  • Does this post answer your question? http://stackoverflow.com/a/1287357/248756 – Michiel Pater Feb 09 '16 at 16:38
  • 2
    Note that the first two are for C-style strings - only #3 creates a C++ string. See also [this question](http://stackoverflow.com/questions/801209/char-vs-stdstring-in-c). – Paul R Feb 09 '16 at 16:38
  • The first two are identical. You're missing a possibility though: `char *greeting = "hello";` and the similar `const char *greeting = "hello";` – Mark Ransom Feb 09 '16 at 16:40
  • 4
    @MarkRansom `char *greeting = "hello";` is deprecated since the dawn of C++. Better don't even mention it to beginners, it only hurts to know one can do that. – Baum mit Augen Feb 09 '16 at 16:43
  • 1
    @BaummitAugen a complete answer would state that explicitly, just to make sure a newbie doesn't try it accidentally. – Mark Ransom Feb 09 '16 at 16:44
  • Maybe this could help too : http://www.cplusplus.com/reference/string/string/ – ahesa Feb 09 '16 at 16:46
  • 1
    "in every case" is a vague term. For example, if your case is "I realized the string was wrong and need to change it", the first two don't give the same result, because the first is far harder to change. – Sebastian Redl Feb 09 '16 at 17:01
  • TL;DR: Use 3), read a book, search the thousands of existing questions and answers about C++ strings next time please. – Christian Hackl Feb 09 '16 at 17:03
  • Take a look at the [string] tag-wiki: http://stackoverflow.com/tags/string/info – Deduplicator Feb 09 '16 at 17:43

2 Answers2

5

1) and 2) work exactly the same. Both create a 6-element non-heap-allocated array, and copy the characters 'h', 'e', 'l', 'l', 'o', '\0' to the array at runtime or load time.

3) creates an instance of std::string and calls its constructor which copies the characters 'h', 'e', 'l', 'l', 'o'(, '\0')* to its internal memory buffer. (* The '\0' is not required to be stored in the memory buffer.)

There is another way to declare a string in C++, using a pointer to char:

const char* greeting = "hello";

This will not copy anything. It will just point the pointer to the first character 'h' of the null-terminated "hello" string which is located somewhere in memory. The string is also read-only (modifying it causes undefined behavior), which is why one should use a pointer-to-const here.

If you're wondering which one to use, choose std::string, it's the safest and easiest.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • 1
    1 and 2 create objects on the stack **only if** they're local variables. If they're globals they go in static storage, and can be initialized at load time, i.e., without any runtime overhead. – Pete Becker Feb 09 '16 at 16:53
  • Instead of using *stack* I would suggest *objects with automatic storage duration.* – NathanOliver Feb 09 '16 at 16:56
1

Do these methods of making strings work exactly the same way and give the exact same results always in every case?

The first two are array definitions:

char greeting [6] = { 'h','e','l','l','o','\0' };
char greeting [ ] = "hello";

"work the same" as in the second definition a '\0' is appended implicitly.

As for the third definition:

string greeting = "hello";

A string is a class type object and as such it is more complex than a simple array.

Is there any difference in the result in any of them?

There is a quantitative1 and qualitative2 difference between the first two and the third stemming from the fact that std::string is a class type object.


1. Quantitative: arrays occupy less memory space than string.

2. Qualitative: string provides resource management and many facilities for element manipulation.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
Ziezi
  • 6,375
  • 3
  • 39
  • 49