When I start this program and open the serial monitor I usually get a bit of garbage message which includes print statements that are part of my code. This usually only happens at the beginning but it is a little annoying that I can not figure this out.
// A derivation of a code I learned from Jack Purdum. One of the best instructors of Arduino C I have encountered.
#include <stdio.h>
#define pause 5000
#define readingPause 8000
int number = 5; // Define and declare an int variable
int *ptrNumber = NULL; // Define and declare an int pointer
void setup () {
Serial.begin(9600);
}
void loop(){
Serial.println ("We have defined an int variable called number. It has an lvalue address and an rvalue which is what is held at that address.");
delay(readingPause);
Serial.println ("We have also defined a int pointer called ptrNumber which has a 2byte address and an rvalue which is meant to hold a variables lvalue address.");
delay (readingPause);
Serial.println ("We have set the rvalue to null in order to avoid junk data.");
delay(pause);
Serial.println ("Here are the results.");
delay(pause);
Serial.print("The lvalue for ptrNumber is ");
Serial.println((long)&ptrNumber,DEC);// The long is there to tell the compiler the data type we wish to use for the ptrNumber address, DEC instructs the reporting in base 10.
delay(pause);
Serial.print("The lvalue for number is ");
Serial.println ((long)&number,DEC);
delay(pause);
Serial.print("The rvalue for our pointer is ");
Serial.println((long)ptrNumber,DEC);
delay(pause);
Serial.print("The rvalue for number is ");
Serial.println (number);
delay(8000);
ptrNumber = &number;// Point the pointer at the lvalue of 'number'. This should make the pointer r value 'number's lvalue
/*
Now whend we querry the processor as to the lvalue of number and the rvalue of ptrNumber we should get the same thing.
*/
Serial.println ("Now we point the pointer at number using the address operator &.");
delay(readingPause);
Serial.println ("The code looks like ptrNumber = &number ");
delay(pause);
Serial.println ("Here are the results:");
delay (pause);
Serial.print("The lvalue for ptrNumber is ");
Serial.println((long)&ptrNumber,DEC);
delay(pause);
Serial.print("The lvalue for number is ");
Serial.println ((long)&number,DEC);
delay(pause);
Serial.print("The rvalue for our pointer is now ");
Serial.println((long)ptrNumber,DEC);
delay(pause);
Serial.print("The rvalue for number is still ");
Serial.println (number);
delay(pause);
Serial.println("As you can see the rvalue for the pointer is the same as the lvalue of the pointers target. This is the result of pointing the pointer.");
delay(readingPause);
/* Now we will use the indirection operator to allow the pointer to take control of the r value held at numbers address*/
Serial.println ("Now we use the indirection operator to allow our pointer to take control of the r value held at numbers address.");
delay(readingPause);
Serial.println ("We will instruct the pointer to change the rvalue of number from 5 to 10 using the indirection operator");
delay(readingPause);
Serial.println ("The code looks like: *ptrNumber = 10 ");
delay(pause);
Serial.println ("Here are the results: ");
*ptrNumber = 10;
delay(pause);
Serial.print("The lvalue for ptrNumber is ");
Serial.println((long)&ptrNumber,DEC);
delay(pause);
Serial.print("The lvalue for number is ");
Serial.println ((long)&number,DEC);
delay(pause);
Serial.print("The rvalue for our pointer is still ");
Serial.println((long)ptrNumber,DEC);
delay(pause);
Serial.print("The rvalue for number is now ");
Serial.println (number);
delay(pause);
Serial.println ("In this example we defined and declared a int variable. We then defined a pointer and set its rvalue to null.");
delay(readingPause);
Serial.println ("Next we pointed the pointer using the address operator &. This placed the target variables lvalue in the pointers rvalue.");
delay(readingPause);
Serial.println ("This ultimately gives the pointer control over the r value held at the variables address.");
delay(readingPause);
Serial.println ("Finally we gave the pointer the power to change the r value of the variable by using the indirection operator *.");
delay(readingPause);
Serial.println ("Most importantly, one must remember that all this is possible because the pointer had the variables address, which is held in the pointers rvalue.");