/* This program demonstrates * 1. Arrays fo C++ string objects * 2. c++ string object functions * 3. Introduction to pointers * * We can declare arrays of C++ string objects. While their memory storage is quite * different from other conventional arrays. the rest of the array principles still * apply. * Their capacity is determined at compile time, they can be initialized with set * notation, we can iterate through them and use each "element" as ca complete C++ * string object. When passed into a function, an individual C++ string object would * pass by value, but an array of C++ string objects would pass by address * * Pointers are variables that can hold the address of another variable. */ #include #include #include using namespace std; int main() { /* An array os C++ string objects */ string stArray[10] = {"Spongebob", "Squidward"}; /* In the C++ string object array, each "Element" is a C++ string object * which is internally a c-string. So, we can use the second, non-obvious, dimension * to access individual characters. */ cout<<"Access individual characters, stArray[0][4]: "<< stArray[0][4]< null pointer exception - will crash // We cannot assign pointers to integer literals //ptr = 0xffffcc3c; // error /* Valid R-values for pointer assignment: * 1. Address of variables of the same type * 2. null * 3. pointer of the same type * 4. reinterpret cast */ // We can assign a pointer to another pointer o the same type int *p2; int anothervar = 15; p2 = &anothervar; ptr = p2; cout<<"After re-assignment, *ptr: "<< *ptr<(dblPtr); cout<<"After reinterpret cast: ptr: "<