7

I need a deep technical explanation of what I'm about to ask, not a solution.

Ive been learning pointers for a week now, I understand it pretty well. But while writing a program, I stumbled upon this error:

cannot convert ‘const std::string’ to ‘const char*’ for argument ‘2’ to ‘char* strcpy(char*, const char*)’

So I solved pretty easily with string.c_str() no problem. But I got very interested into why this is. I have been searching like crazy why a const string is not the same a const char *. When people explain a string they say its no different than a char *, so why does adding a const before the string not make it a const char *?

Xymostech
  • 9,710
  • 3
  • 34
  • 44
userX
  • 370
  • 3
  • 11

1 Answers1

11

string is an object meant to hold textual data (a string), and char* is a pointer to a block of memory that is meant to hold textual data (a string).

A string "knows" its length, but a char* is just a pointer (to an array of characters) -- it has no length information. Therefore, in order for you to be able to deduce the length of a "string" represented by a char*, you must terminate it with something special, which is conventionally the null character '\0' in C.

But a string doesn't terminate itself with '\0' (it's extra work for no benefit), so the question becomes: what if you need to convert between the two formats?

Converting from a char* to a string can happen implicitly -- string has a constructor just for that purpose. But to go the other way around, you have to tell the string object to null-terminate itself and give you a valid pointer for your purpose. (It won't do that implicitly because it can require extra work and/or lead to accidents in code.)

user541686
  • 205,094
  • 128
  • 528
  • 886
  • 1
    @reconx86: You're confusing two different things: *`std::string`* is an object, but "a string" is just a word that means "a piece of text". You can *represent* "a string" (which means "text") in any way you want, and typically, they represent it using a `char*`, a `std::string` object, or something along those lines. Make sure you understand what the book is talking about -- the object, or the abstract idea. – user541686 Apr 01 '13 at 05:44
  • @reconx86: *You're* referring to `std::string`, yes, but I'm pretty sure the books aren't doing the same when they say "a string" is "nothing more than a char pointer". – user541686 Apr 01 '13 at 05:51
  • *"I always thought of a string as char array with another keyword as nickname."* No, I just explained to you what it is, above. Forget what you've already learned before, and read it again -- you're confusing C arrays, the general notion of "arrays", pointers, `std::string`, and the general notion of "strings" with each other. And note that you used the word "keyword" incorrectly; you should have said "identifier". Regarding *"does \0 terminating a string make a char array?"*, it doesn't make sense -- you're confusing the possible **implementations** of strings with the general **concept**. – user541686 Apr 01 '13 at 06:19
  • Ive read thru all the comments once more I understand what you mean. And I know you meant there is a difference between the object and the representation. You were also right about the book I didn't focus on that while I was reading the book. – userX Apr 01 '13 at 06:41
  • I think I have a better understand of what a string its a char array wrapped into a class. And thats why it doesn't exactly function like a char array. http://stackoverflow.com/questions/1287306/difference-between-string-and-char-types-in-c http://stackoverflow.com/questions/801209/c-char-vs-stdstring – userX Apr 01 '13 at 06:52