Demonstrates the use of pointers with scanf.
Fork of RawSerial_ex_2 by
main.cpp@1:3e364d2f184a, 2017-11-05 (annotated)
- Committer:
- CSTritt
- Date:
- Sun Nov 05 20:25:25 2017 +0000
- Revision:
- 1:3e364d2f184a
- Parent:
- 0:3b040f367dd8
Initial version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 1:3e364d2f184a | 1 | /* |
CSTritt | 1:3e364d2f184a | 2 | Project: PointerScanf |
CSTritt | 1:3e364d2f184a | 3 | File: main.cpp |
CSTritt | 1:3e364d2f184a | 4 | Revised by Dr. C. S. Tritt |
CSTritt | 1:3e364d2f184a | 5 | 11/3/17 |
CSTritt | 1:3e364d2f184a | 6 | |
CSTritt | 1:3e364d2f184a | 7 | Use a pointer, rather than a reference in scanf. Based on a Horton, |
CSTritt | 1:3e364d2f184a | 8 | Chapter 7 example. |
CSTritt | 1:3e364d2f184a | 9 | */ |
mab5449 | 0:3b040f367dd8 | 10 | #include "mbed.h" |
mab5449 | 0:3b040f367dd8 | 11 | |
CSTritt | 1:3e364d2f184a | 12 | // Use an explicit Serial object (as opposed to the implicit one) for a change |
CSTritt | 1:3e364d2f184a | 13 | // of pace... |
CSTritt | 1:3e364d2f184a | 14 | Serial pc(USBTX, USBRX); |
mab5449 | 0:3b040f367dd8 | 15 | |
CSTritt | 1:3e364d2f184a | 16 | int main(void) |
CSTritt | 1:3e364d2f184a | 17 | { |
CSTritt | 1:3e364d2f184a | 18 | int value = 0; |
CSTritt | 1:3e364d2f184a | 19 | int *pValue = &value; // Set pointer to refer to value. |
CSTritt | 1:3e364d2f184a | 20 | pc.printf ("\nTurn local echo on.\n"); |
CSTritt | 1:3e364d2f184a | 21 | pc.printf ("Enter an integer: "); |
CSTritt | 1:3e364d2f184a | 22 | pc.scanf(" %d", pValue); // Read into value via the pointer. |
CSTritt | 1:3e364d2f184a | 23 | pc.printf("You entered %d (base 10).\n", value); // Display the value entered. |
CSTritt | 1:3e364d2f184a | 24 | pc.printf("Which is %x (base 16).\n", value); // Display the value entered. |
CSTritt | 1:3e364d2f184a | 25 | pc.printf("Done. Now I sleep.\n"); |
CSTritt | 1:3e364d2f184a | 26 | while (true) wait(3600.0f); |
CSTritt | 1:3e364d2f184a | 27 | } |