// ----------------------------- // string.cpp // ----------------------------- #include "w.h" /* WARNING Strings are considered to be poorly supported by C++, but they do allow some non-typical manipulations, which won't work on other types of array. For example: char *s = "Hello". */ void main() { nl(0); banner("string.cpp"); char c[80] = "string array"; p("c = ");pl(c); // c = "new string array"; // WRONG ! '=' can't do that strcpy(c, "new string array"); // OK p("c = "); pl(c); char *s = "string"; // memory on the stack p("s = "); pl(s); s = "new string"; p("s = ");pl(s); // char strings are null-terminated arrays char b[] = {'B','e','e','t','h','o','v','e','n','\0'}; p("b = "); pl(b); // b accepted as char* by pl nl(); // char *sc; // dangerous ! sc should point somewhere char *sc = new char[80]; // memory on the heap strcpy(sc,s); // strcpy is from p("sc = "); pl(sc); delete [] sc; // return the memory char c2[80]; strcpy(c2,s); p("c2 = "); pl(c2); strcat(s," for your eyes only"); p("s = "); pl(s); } /* output ---------------------------------- ------------ string.cpp ------------ c = string array c = new string array s = string s = new string b = Beethoven sc = new string c2 = new string s = new string for your eyes only */