Simple training demonstration to show the use of union and structure

Dependencies:   USBDevice mbed

Fork of OddExample1 by Jon Fuge

Revision:
6:5f4be4b8db14
Parent:
5:eb5c6ae5938b
Child:
7:4753526f342a
--- a/main.cpp	Tue Nov 26 11:18:11 2013 +0000
+++ b/main.cpp	Tue Nov 26 11:39:57 2013 +0000
@@ -1,5 +1,5 @@
 /*******************************************************************************
-* This program demonstrates the use of pointers                                *
+* This program demonstrates the use of structures                              *
 *                                                                              *
 * Jon Fuge                                                                     *
 * V1.0 25/11/2013 First issue of code                                          *
@@ -10,21 +10,22 @@
 
 USBSerial serial; // Virtual serial port over USB. Use Teraterm as the interface
 
+struct coordXY { int iX; int iY; }; // Define a new structure
+
 int main() {
-   char cBox1 = 5;
-   char cBox2 = 10;     // Declare two variables.
-   char *ptr  = &cBox1; // Declare a pointer directed to cBox1
+   coordXY Position1, Position2;      // Declare variables with the new structure
    
-   wait (10);           // Wait 10 seconds to connect port
-
-   serial.printf("cBox1:%i, cBox2:%i, *ptr:%i\n\r", cBox1, cBox2, *ptr);
+   wait (10);             // Wait 10 seconds to connect port
 
-   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
+   Position1.iX = 1;      // Sets Position1.iX only
+   Position1.iY = 2;      // Sets Position1.iY only
+   Position2.iX = 9;      // Sets Position2.iX only
+   Position2.iY = 6;      // Sets Position2.iY only
 
-   for(;;) {}           // Loop forever
+   serial.printf("Position1.iX:%i, Position1.iY:%i\n\r", Position1.iX, Position1.iY);
+
+   Position1 = Position2; // Directly load Position1 from Position2
+   serial.printf("Position1.iX:%i, Position1.iY:%i\n\r", Position1.iX, Position1.iY);
+
+   for(;;) {}             // Loop forever
 }
\ No newline at end of file