1

I was faced with this question for a C++ exam and as much as I think I know the answer, the question confused me enough that I rather have a "Second opinion" per say... haha.

a) The literal "Hello, World!" is a C-Style string. What is its type?

b) Given that the type of C-style string and the type std::string are different, how is it possible that the line std::string hello("Hello,World!"); compiles without errors/warnings (assuming the <string> header has been included)?

From my understanding the C-Style string is of type (a) array of chars while std::string is of type string... and (b) the reason it compiles is that in C++ the whole casting to chars and all that is done "under the hood" by the type string rather than "in the code".

Am I correct?

Thanks!

Community
  • 1
  • 1
Gal Appelbaum
  • 1,905
  • 6
  • 21
  • 33
  • Side note: I suggest you forget the type "string" as it does not exist, it is just a handy representation of an array of characters. Basically it stores the characters and remembers the length (it may store it in different chunks although I don't know any implementation of std::string that actually does that). That's why there're interchanged easily. – demorge Feb 28 '12 at 11:13
  • @demorge: for what it's worth, non-contiguous storage for `std::string` (or any other instantiation of `std::basic_string`) has been banned in C++11. – Steve Jessop Feb 28 '12 at 11:26

2 Answers2

6

What you say is probably close enough to be called "true", although you're missing some detail. The type of the literal "Hello, World!" in C++ is const char[14]. As you say that's an array of chars, but the size is part of the type.

std::string has a constructor that takes const char*, and there's an implicit conversion from const char[14] to const char*. Those two together handle (b).

There's no "casting to chars" involved, though.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • possible to assume that above literal have type `static const char[14]` according to http://stackoverflow.com/questions/349025/is-a-string-literal-in-c-created-in-static-memory ? – triclosan Feb 28 '12 at 11:11
  • 1
    @triclosan: you're right that string literals have static storage duration, but storage class specifiers aren't part of the type. – Steve Jessop Feb 28 '12 at 11:18
  • yea, I didn't know how to explain myself too well back there lol. Thanks Steve! – Gal Appelbaum Feb 28 '12 at 11:19
  • 1
    @triclosan: `static` is a linkage modifier, it is not part of the object's type. – Philipp Feb 28 '12 at 11:22
2

In C++, the type of "Hello, World!" is an array of 14 const char and in C the type is an array of 14 char. The trailing null character of the string is a part of the array.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • what I get from what u said is that in C++ you couldn't change 1 char in the "Hello, World!" string while in C style you could technically switch a character manually since its just an array of char(s) rather than const char(s)? – Gal Appelbaum Feb 28 '12 at 11:21
  • 1
    @Gal: it's still undefined behavior in C to modify a string literal (i.e: it's forbidden by the language), but the type system doesn't stop you. The authors of the C standard have more concern than the authors of the C++ standard for pre-standard or defective C code that takes a `char*` parameter where it could take a `const char*`, for an argument that it doesn't modify. If existing code passes in a string literal, then changing the type of the string literal from `char[N]` to `const char[N]` breaks the code. C never made that breaking change, despite `const` being part of the first standard. – Steve Jessop Feb 28 '12 at 11:31