I’m interested in how the array to which the pointer looks like.
As I understand it, the function takes a string for example string str = “12345” and turns it into an array of charms {‘1’, ‘2’, ‘3’, ‘4’, ‘5’, \ 0} and then returns a pointer to this array
Or so {“12345”}?
And another such moment, if it returns a pointer, then why does this work:
cout << str.c_str() <<endl;
Answer 1, authority 100%
As I understand it, string::c_str
returns a pointer to the area of memory where it stores the string itself. That is, when returning, it does not create any additional arrays, nothing converts or copies anywhere.
About cout << str.c_str() <<endl;
: cout
prints pointers like char *
or const char *
fine like strings. You won’t be confused by the cout << "12345" << endl;