/* Things to take away from this code. * 1) Pointer initialization * 2) Pointer arithmetic * 3) Array/pointer duality * 4) Modern strings are way better than null-terminated strings */ #include using namespace std; // An array of chars can be initialized from a null-terminated string char a_string[] = "Hello, world"; int main() { char *p = NULL; // NULL (and 0) can be used to initialize any // pointer p = a_string; // a_string, the variable, is a pointer to the first element of // a_string, the array. Ergo it's type is char* so we're fine. // This iterates over the elements of a null terminated string // printing out each character. while (*p != '\0') { cout << *p; /* Below is an example of pointer arithmetic. Arithmetic on a pointer calculates in units of elements. So the below moves up one element, no matter what type of element the pointer points at. Also, notice that this computation works whether the pointer points to a single elemnt or the pointer comes from an array. Put another way, we can shoot ourselves in the foot with this. */ p = p + 1; } cout << endl; p = a_string; for (int i = 0; i < 12; i++) { /* p[i] is converted into a deref of memory location (p + (i * sizeof(el type))). */ cout << p[i]; } cout << endl; // The key here is that since pointers can use the array syntax for access // and an array variable generates a pointer to the first element, // arrays and pointers are often considered interchangeable to old // time C/C++ programmers. p = a_string; // For Jedi Hackers only /* Prints out the characters of a null terminated string p * binds tighter than the ++ operator * Grab the element pointed at, stop if it's '\0' * Increment the pointer * Then print out the previous element * Note that this is *not* recommended style * Really ancient C/C++ hackers and/or code may do this * Why? Perception that this translates into fast compiled code. */ while (*p++) cout << p[-1]; // Padawan version // while (*p) cout << p++; cout << endl; // Enter The Clone Wars // We want to copy a_string into heap space. // Need 12 bytes for the visible chars, one for the null terminator p = new char[13]; // Unlucky 13? for (int i = 0; i < 13; i++) { p[i] = a_string[i]; } cout << p << endl; // Reverse, reverse // Let's reverse a string // Standard library strings are way more convenient than null // terminated strings. Use modern strings unless some moron makes you // use old style strings, like in homework assignments. /* About the only thing they're good for now, is in building dynamically * sized data structures. That's what the KonsTest.cpp code demonstrates. */ // Modern strings can be initialized from old style strings. string another_string("Hello, World"); // Modern strings support nice features, like a length member // function that tells you how many characters are in the string. for (int i = another_string.length() - 1; 0 <= i; i--) { // At the same time, they look like null-terminated string. cout << another_string[i]; } cout << endl; return 0; }