// This is a step by step description of how pointers work in arduino
// Pointers allow for more efficient use of memory
// They can improve speed in intensive, memory access hungry applications
// Explanations are printed along with the output in the console to aid digestion of the information
// This is very primitive to help people like me get their heads round pointers

void setup() {
  Serial.begin(9600);
  Serial.println("");
  Serial.println("Text above this line in the console is start up text from the board turning on. Please ignore.");
  Serial.println("");
  Serial.println("This is a step by step description of how pointers work in arduino");
  Serial.println("Pointers allow for more efficient use of memory");
  Serial.println("They can improve speed in intensive, memory access hungry applications");
  Serial.println("Explanations are printed along with the output in the console to aid digestion of the information");
  Serial.println("This is very primitive to help people like me get their heads round pointers");
  Serial.println("");
  Serial.println("Firstly a pointer p is declared to point to a data type int");
  // Serial.println("");
  Serial.println("    int *p;");
  Serial.println("");

  int *p;

  Serial.println("Then a variable i is declared and set to a value of 12");
  Serial.println("    int i = 12;");
  Serial.println("");

  int i = 12;

  Serial.println("Pointer p is configured to look at the memory location of variable i");
  Serial.println("    p = &i;");

  p = &i;

  Serial.println("");
  Serial.println("Using the code \"Serial.println(i);\" we can print the value of i,  i is currently set to: ");
  Serial.print  ("    ");  //added for padding

  Serial.println(i);

  Serial.println("");
  Serial.println("The code \"Serial.println(uint32_t(p), HEX);\" gives us the location of variable i, which is: ");
  Serial.print  ("    ");  //added for padding

  Serial.println(uint32_t(p), HEX);

  Serial.println("");
  Serial.println("Using the code \"Serial.println(*p);\", the value of variable i can now be found using the pointer p:");
  Serial.print  ("    ");  //added for padding

  Serial.println(*p);

  Serial.println("");
  Serial.println("A new variable j is declared with no value");
  Serial.println("    int j;");
  Serial.println("");

  int j;        // new variable j

  Serial.println("Using the code \"j = *p;\", variable j is defined to whatever value pointer p is pointing at.");

  j = *p;

  Serial.println("");
  Serial.println("with the code \"Serial.println(j);\" we can print the value of j,  which is now set to: ");
  Serial.print  ("    ");  //added for padding

  Serial.println(j);

  Serial.println("");
  Serial.println("with the code \"*p = 15;\" we can change the value at this memory location to 15.");
  Serial.println("    Remember that pointer p is currently looking at variable i");

  *p = 15;

  Serial.println("");
  Serial.println("You will see that this has updated the variable i which we can read with the code \"Serial.println(i);\":");
  Serial.print  ("    ");  //added for padding

  Serial.println(i);

  Serial.println("");
  Serial.println("Using \"Serial.println(*p);\", we can find the same value by referencing pointer p:");
  Serial.print  ("    ");  //added for padding
  Serial.println(*p);

  Serial.println("");
  Serial.println("with the code \"p = &j;\" we can change the variable that pointer p is pointing at.");

  p = &j;       // set the pointer to look at the location of variable j

  Serial.println("");
  Serial.println("finally, again using \"Serial.println(*p);\", we can now see that pointer p has a new value taken from the memory location of variable j:");
  Serial.print  ("    ");  //added for padding
  Serial.println(*p);

  Serial.println("");
  Serial.println("** Please scroll the console up to see all the text **");
  Serial.println("");
}

void loop() {

  delay(100000);
}