Charles Tritt / Mbed 2 deprecated ArrayPointers

Dependencies:   mbed

Fork of FunctionPointers by Charles Tritt

Revision:
1:bdea7966ea78
Parent:
0:f106e3c0c589
Child:
2:7d14b75d8a68
diff -r f106e3c0c589 -r bdea7966ea78 main.cpp
--- a/main.cpp	Sun Nov 05 20:27:53 2017 +0000
+++ b/main.cpp	Sun Nov 05 21:07:10 2017 +0000
@@ -1,51 +1,31 @@
 /*
-    Project: FunctionPointer
+    Project: ArrayPointers
     File: main.cpp
     Modified by: Dr. C. S. Tritt
     Last revised: 11/5/17 (v. 1.0)
 
-    Based on Horton Program 9.1 Pointing to functions.
+    Based on Horton Program 7.4 Arrays and pointers.
 */
 #include "mbed.h"
 
-// Function definitions (a.k.a., prototypes).
-int sum(int, int);
-int product(int, int);
-int difference(int, int);
-
+//
 int main(void)
 {
-    int (*pfun)(int, int); // Local function pointer declaration.
-    int a = 10;  // Initial value for a.
-    int b = 5;  // Initial value for b.
-
-    pfun = sum; // pfun now points to function sum().
-    int result = pfun(a, b); // Call sum() through pointer.
-    printf("pfun = sum result = %d\n", result);
+    char myString[] = "My text."; // Initialize a string.
+    printf("\nString content (direct): %s\n", myString);
 
-    pfun = product; // pfun now points to function product().
-    result = pfun(a, b); // Call product() through pointer.
-    printf("pfun = product result = %d\n", result);
+    char* p = &myString[0]; // Take the address of the 1st element.
+    printf("String content (explicit reference): %s\n", p);
 
-    pfun = difference; // pfun now points to function difference().
-    result = pfun(a, b); // Call difference() through pointer.
-    printf("pfun = difference result = %2d\n", result);
+    p = myString; // A plain array name is a pointer to its 1st element.
+    printf("String content (implied reference: %s\n", p);
 
-    while (true) wait(3600.0f); // Wait forever, over and over.
-}
-
-// Function declarations...
-int sum(int x, int y)
-{
-    return x + y;
-}
-
-int product(int x, int y)
-{
-    return x * y;
-}
-
-int difference(int x, int y)
-{
-    return x - y;
+    // Here's another way to initialize a string. This is actually the 
+    // approach recommended by Kieras at Michigan as it may avoid the creation 
+    // of a literal followed by copying to the array...
+    char* pMyOtherString = "Here is more text.";
+    printf("Other string: %s\n", pMyOtherString);
+    
+    printf("Done. I sleep now.\n");
+    while(true) wait(3600.0f);
 }
\ No newline at end of file