// // main.cpp // class15 // // Created by Chad Spensky on 8/9/16. // Copyright © 2016 Chad Spensky. All rights reserved. // #include using namespace std; // Let's define our own types! typedef int chads_type; typedef int* intPtr; int main(int argc, const char * argv[]) { // Declare a few integers int a = 4; int b = 3; int c = 2; int integer = 1; // Declare a few pointers int* integer_ptr = &integer; int *integer_ptr2 = &integer; // Let's print the values of our integer cout << "Our int: " << integer << endl; cout << "Int pointer: " << integer_ptr << " and " << integer_ptr2 << endl; // Let's dereference a pointer cout << "*Pointer: " << *integer_ptr << endl; // Change the value integer = 100; cout << "Our int: " << integer << endl; cout << "*Pointer: " << *integer_ptr << endl; // Change it with the pointer *integer_ptr = 200; cout << "Our int: " << integer << endl; cout << "*Pointer: " << *integer_ptr << endl; // What exactly is an array anyway...? cout << "integer_ptr[0] = " << integer_ptr[0] << endl; cout << "integer_ptr[1] = " << *(integer_ptr+1) << endl; cout << "integer_ptr[2] = " << integer_ptr[2] << endl; cout << "integer_ptr[3] = " << integer_ptr[3] << endl; cout << "integer_ptr[4] = " << integer_ptr[4] << endl; // Can we even change values like this? integer_ptr[1] = 5; cout << "c = " << c << endl; cout << "WTFBBQ?!?!?!" << endl; // Let's de-reference c to get to the bottom of this... cout << "Int pointer: " << integer_ptr << endl; cout << "Int pointer + 1: " << integer_ptr+1 << endl; cout << "&c: " << &c << endl; // Dynamic pointers! int *dyn_ptr = new int; *dyn_ptr = 15; cout << "dyn_ptr = " << dyn_ptr << endl; cout << "*dyn_ptr = " << *dyn_ptr << endl; // Let's delete it delete dyn_ptr; // Look what happens when we create a new dynamic pointer // It reuses the memory we just freed int *dyn_ptr2 = new int; *dyn_ptr2 = 16; cout << "dyn_ptr = " << dyn_ptr << endl; cout << "dyn_ptr2 = " << dyn_ptr2 << endl; cout << "*dyn_ptr = " << *dyn_ptr << endl; cout << "*dyn_ptr2 = " << *dyn_ptr2 << endl; // Time to use our own types chads_type array_size = 3; cout << "array_size = " << array_size << endl; // Let's make a dynamic array cout << "Enter the size of the array:" << endl; cin >> array_size; intPtr dyn_array = new int[array_size]; // Set the value at each index to the index value +1 for (int i = 0; i < array_size; i++) { *(dyn_array+i) = i+1; } // Print the contents of the array for (int i = 0; i < array_size; i++) { cout << dyn_array[i] << " "; } cout << endl; return 0; }