Simple training demonstration to show the use of pointers
Fork of EnumerationExample by
Revision 5:eb5c6ae5938b, committed 2013-11-26
- Comitter:
- jf1452
- Date:
- Tue Nov 26 11:18:11 2013 +0000
- Parent:
- 4:c4e451a1890f
- Commit message:
- Pointer Example
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r c4e451a1890f -r eb5c6ae5938b main.cpp --- a/main.cpp Tue Nov 26 09:52:16 2013 +0000 +++ b/main.cpp Tue Nov 26 11:18:11 2013 +0000 @@ -1,5 +1,5 @@ /******************************************************************************* -* This program demonstrates the use of enumeration * +* This program demonstrates the use of pointers * * * * Jon Fuge * * V1.0 25/11/2013 First issue of code * @@ -8,16 +8,23 @@ #include "mbed.h" #include "USBSerial.h" -enum DaysOfTheWeek { SUN , MON, TUE, WED, THUR, FRI, SAT }; -enum MonthsOfTheYear { JAN = 1, FEB, MAR, APR, MAY, JUN,\ - JUL, AUG, SEP, OCT, NOV, DEC}; - USBSerial serial; // Virtual serial port over USB. Use Teraterm as the interface int main() { - wait (10); // Wait 10 seconds to connect port + char cBox1 = 5; + char cBox2 = 10; // Declare two variables. + char *ptr = &cBox1; // Declare a pointer directed to cBox1 + + wait (10); // Wait 10 seconds to connect port + + serial.printf("cBox1:%i, cBox2:%i, *ptr:%i\n\r", cBox1, cBox2, *ptr); - serial.printf("Sunday is day %i, April is the %i month of the year\n\r", SUN, APR); + cBox2 = cBox1; // cBox2 was 10, but is now 5 + serial.printf("cBox1:%i, cBox2:%i, *ptr:%i\n\r", cBox1, cBox2, *ptr); + + cBox1 = 15; // cBox1 was 5, but is now 15 + serial.printf("cBox1:%i, cBox2:%i, *ptr:%i\n\r", cBox1, cBox2, *ptr); + // cBox2 will still report 5, but *ptr now reports 15 - for(;;) {} // Loop forever + for(;;) {} // Loop forever } \ No newline at end of file