/* Lecture Code 2.0
 *
 * Introduction to pointers and references.  Comments have been added to the relevant sections.
 * Note that this code will probably crash if you run it, since it contains several mistakes!
 */

#include <iostream>
#include <string>
#include <cstddef> // For NULL
using namespace std;

/* Simple function to modify a parameter. */
void Intensify(string& str)
{
	str += "er";
}

int main()
{
	int myInteger, myOtherInteger;
	
	int* myPtr;
	int* myOtherPtr;
	
	string* myStringPointer;
	string myString;

	/* Assign myPtr and myStringPointer the addresses of myInteger and myString,
	 * respectively.  The pointers now point to the appropriate variables.
	 */
	myPtr = &myInteger;
	myStringPointer = &myString;

	/* Mutate myInteger indirectly by storing the value 137 in it through
	 * myPtr.
	 */
	*myPtr = 137;

	/* These will print the same value because myPtr points to myInteger. */
	cout << myInteger << endl;
	cout << *myPtr << endl;

	/* Simple pointer assignment - make myPtr and myOtherPtr both point to
	 * myOtherInteger.
	 */
	myPtr = &myOtherInteger;
	myOtherPtr = myPtr;

	/* Uh oh... ohnoes is not initialized!  It now points to a random memory
	 * location and this code will almost certainly fail to check that ohnoes
	 * isn't pointing anywhere.  Make sure to initialize your pointers!
	 */
	int* ohnoes;
	if(ohnoes == NULL)
		cout << "ohnoes == NULL" << endl;
	else
		cout << *ohnoes << endl;

	/* Dynamically-allocate some memory and store it in myNewPtr. */
	int* myNewPtr = new int;
	*myNewPtr = 137;
	cout << *myNewPtr << endl;

	/* Make sure to clean up memory you allocate! */
	delete myNewPtr;
	myNewPtr = NULL; // Just to be safe.
	return 0;
}