Are these assumptions correct as far as dynamic strings are concerned?
char* str3 = new char[5];
str3[3] = 'A'; //run-time heap memory
str3[15] = 'A'; //run-time heap memory
str3 = "Robert"; //Pointer now pointing to a string literal requiring static stack memory
Thanks a lot for your help...
The type of a string literal is "array of the appropriate number of const characters," so "Bohr" is of type const char[5]."
Stroustrup pg.90
Doesn't this imply a string literal is const char[]?
//char* to new char instantiation
char* str = new char;
char* str2 = new char[];
char* str3 = new char[5];
//char* to new char assignment
str3[0] = 'z'; //character assignment
str3 = "matt"; //string assignment
str3 = "mattttttty"; //resizable
It looks like char* to new gives you the best of...
The C++ Programming Language by Stroustrup on page 90 makes a passing reference to your question.
Speaking of char* string literals:
"It is... an error to try to modify a string literal through such a pointer."
char* p = "Plato";
p[4] = 'e'; //error: assignment to const; result is undefined...
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.