#include #include using namespace std; int main() { string cppStyle = "Lilly McSilly"; // ^^^^^^^^^^^^^^^ c-style string literal cout << cppStyle << endl; // char* cStyle = cppStyle.c_str(); // compilation not allowed! const char* cStyle = cppStyle.c_str(); //char* cStyle2 = "Lilly McSilly"; // this will lead to bus error //cStyle2[0] = 'M'; char cStyle2[] = "Lilly McSilly"; // this will make a local copy for us cStyle2[0] = 'M'; cout << cStyle2 << endl; cout << "==========================" << endl; const char* cStyle3 = "Lilly McSilly"; const char* cStyle4 = "Lilly McSilly"; cout << cStyle3 << endl; // note: prints as a C-style string cout << cStyle4 << endl; cout << *cStyle3 << endl; // note: dereferences to get a single char cout << &cStyle3 << endl; // this is the addy of the variable cStyle3 // note: prints as the address of the string literal -- note that // the addresses (to the string literal) are the same cout << static_cast(cStyle3) << endl; cout << static_cast(cStyle4) << endl; cout << "==========================" << endl; char cStyle5[10] = "foo"; // C++ will put the \0 in for us cStyle5[3] = -97; cStyle5[9] = '@'; for (int i = 0; i < 10; i++) { cout << cStyle5[i] << " -- " << static_cast(cStyle5[i]) << endl; } cout << "<" << cStyle5 << ">" << endl; cout << "==========================" << endl; // pointer arithmetic char* cptr = cStyle5; cout << cptr << endl; cptr++; // "add 1", but it depends on the pointer type cout << cptr << endl; cptr++; // "add 1", but it depends on the pointer type cout << cptr << endl; return 0; }