Charles Tritt / Mbed 2 deprecated StructPlay

Dependencies:   mbed

Fork of ArraySizeof by Charles Tritt

Revision:
1:987d0ca5cf9a
Parent:
0:8482415bf245
Child:
2:761d1c20f740
diff -r 8482415bf245 -r 987d0ca5cf9a main.cpp
--- a/main.cpp	Mon Oct 23 01:15:43 2017 +0000
+++ b/main.cpp	Mon Oct 23 02:29:40 2017 +0000
@@ -1,33 +1,43 @@
 /*
-    Project: GradeAverage
+    Project: ArraySizeof
     File: main.cpp
     Ported to mbed/Nucleo by: Dr. C. S. Tritt
     Last revised: 10/22/17 (v. 1.0)
-    
-    Program 5.5 Using the & operator - from Beginning C, 5th ed. Ivor Horton. 
-    
+
+
 */
 #include "mbed.h"
 
 int main(void)
 {
-  // Define some integer variables.
-  int a = 1;
-  int b = 2;
-  int c = 3;
-
-  // Define some floating-point variables.
-  float d = 4.0f;
-  float e = 5.0f;
-  float f = 6.0f;
-
-  printf("\nVariables of type int occupy %u bytes each.", sizeof(int));
-  printf("\nHere are the addresses of some type int variables --");
-  printf("\nThe address of a is: %p.\nThe address of b is: %p.", &a, &b);
-  printf("\nThe address of c is: %p.", &c);
-  printf("\n\nVariables of type float occupy %u bytes each.", sizeof(float));
-  printf("\nHere are the addresses of some type float variables --");
-  printf("\nThe address of d is: %p.\nThe address of e is: %p.", &d, &e);
-  printf("\nThe address of f is: %p.", &f);
-  while (true) {} // Loop forever when done.
+    // Create a 2-D array.
+    int myNum[3][4] = {
+        { 10, 20, 30, 40 }, // Values for first row
+        { 15, 25, 35, 45 }, // Values for second row
+        { 47, 48, 49, 50 }  // Values for third row
+    };
+    
+    printf("\n\nArray output...\n\n");
+    // Known sizes.
+    for (int r = 0; r < 3; r++) {
+        for (int c = 0; c < 4; c++) {
+            printf("%d   ", myNum[r][c]);
+        }
+        printf("\n");
+    }
+    
+    // For mbed/Nucleo boards, the final \n is generally required to see output.
+    printf("\nSize of myNum = %u.\n", sizeof(myNum)); // Total size.
+    printf("Size of myNum[0] = %u.\n", sizeof(myNum[0])); // Row size.
+    printf("Size of myNum[0][0] = %u.\n\n", sizeof(myNum[0][0])); // Item size.
+    
+    // Calculated sizes.
+    for (int r = 0; r < sizeof(myNum)/sizeof(myNum[0]); r++) {
+        for (int c = 0; c < sizeof(myNum[0])/sizeof(myNum[0][0]); c++) {
+            printf("%d   ", myNum[r][c]);
+        }
+        printf("\n");
+    }
+    
+    while (true) {} // Loop forever when done.
 }
\ No newline at end of file