#include using namespace std; int main() { int foo[3]; foo[0] = 1; foo[1] = 2; foo[2] = 3; int* fooptr = foo; cout << foo << endl; // addy of start of array cout << fooptr << endl; // addy of start of array for (int i = 0; i < 3; i++) { cout << foo[i] << " "; } cout << endl; // next two each set first entry in array foo[0] = 8675309; for (int i = 0; i < 3; i++) { cout << foo[i] << " "; } cout << endl; *fooptr = 5551212; for (int i = 0; i < 3; i++) { cout << foo[i] << " "; } cout << endl; // next two each set the third entry in array // note that even though foo[2] is 8 bytes from foo[0], we don't // have to do *(fooptr + 8). When the pointer arithmetic is done, // the runtime system multiplies the "2" by 4, because fooptr is a pointer // to a 4 byte type (int in this case) *(fooptr + 2) = 123456789; for (int i = 0; i < 3; i++) { cout << foo[i] << " "; } cout << endl; fooptr[2] = 77777; for (int i = 0; i < 3; i++) { cout << foo[i] << " "; } cout << endl; // will set first entry in the array, and then advance the pointer // to point to the second entry in the array (fooptr++ returns the value // of fooptr before incrementing) *(fooptr++) = 123456789; for (int i = 0; i < 3; i++) { cout << foo[i] << " "; } cout << endl; // note fooptr++ increased value of fooptr by 4, not by 1 cout << fooptr << endl; // will advance the pointer to point to the next entry in the array, // and then set the entry at that position (++fooptr returns the value // of fooptr after incrementing) *(++fooptr) = 123456789; for (int i = 0; i < 3; i++) { cout << foo[i] << " "; } cout << endl; char mychar = 'A'; char* charptr = &mychar; cout << static_cast(charptr) << endl; // Note that because charptr is a pointer to char, incrementing it // using ++ only adds 1 to charptr ++charptr; cout << static_cast(charptr) << endl; string mystring[4]; mystring[0] = "Frosty is a very very good boy!"; // length 32 (including null byte) mystring[1] = "A very good boy!"; // length 17 mystring[2] = "Really!"; //length 8 string* myStringPtr = &mystring[0]; cout << myStringPtr << endl; cout << &mystring[0] << endl; cout << *myStringPtr << endl; ++myStringPtr; cout << myStringPtr << endl; cout << &mystring[1] << endl; cout << *myStringPtr << endl; ++myStringPtr; cout << myStringPtr << endl; cout << &mystring[2] << endl; cout << *myStringPtr << endl; ++myStringPtr; cout << myStringPtr << endl; cout << &mystring[3] << endl; cout << *myStringPtr << endl; // 6F0 = 6 * 16^2 + 15 * 16 = 1776. 708 = 7 * 16^2 + 8 1800. (so it's // original value + 24 (can't allocate 22 bytes). 720 is 1824, so 24 more than // }