The connection between arrays and pointers.
Fork of FunctionPointers by
main.cpp@2:7d14b75d8a68, 2017-11-05 (annotated)
- Committer:
- CSTritt
- Date:
- Sun Nov 05 21:10:29 2017 +0000
- Revision:
- 2:7d14b75d8a68
- Parent:
- 1:bdea7966ea78
Corrected a comment.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 0:f106e3c0c589 | 1 | /* |
CSTritt | 1:bdea7966ea78 | 2 | Project: ArrayPointers |
CSTritt | 0:f106e3c0c589 | 3 | File: main.cpp |
CSTritt | 0:f106e3c0c589 | 4 | Modified by: Dr. C. S. Tritt |
CSTritt | 0:f106e3c0c589 | 5 | Last revised: 11/5/17 (v. 1.0) |
CSTritt | 0:f106e3c0c589 | 6 | |
CSTritt | 1:bdea7966ea78 | 7 | Based on Horton Program 7.4 Arrays and pointers. |
CSTritt | 0:f106e3c0c589 | 8 | */ |
CSTritt | 0:f106e3c0c589 | 9 | #include "mbed.h" |
CSTritt | 0:f106e3c0c589 | 10 | |
CSTritt | 1:bdea7966ea78 | 11 | // |
CSTritt | 0:f106e3c0c589 | 12 | int main(void) |
CSTritt | 0:f106e3c0c589 | 13 | { |
CSTritt | 1:bdea7966ea78 | 14 | char myString[] = "My text."; // Initialize a string. |
CSTritt | 1:bdea7966ea78 | 15 | printf("\nString content (direct): %s\n", myString); |
CSTritt | 0:f106e3c0c589 | 16 | |
CSTritt | 1:bdea7966ea78 | 17 | char* p = &myString[0]; // Take the address of the 1st element. |
CSTritt | 1:bdea7966ea78 | 18 | printf("String content (explicit reference): %s\n", p); |
CSTritt | 0:f106e3c0c589 | 19 | |
CSTritt | 1:bdea7966ea78 | 20 | p = myString; // A plain array name is a pointer to its 1st element. |
CSTritt | 1:bdea7966ea78 | 21 | printf("String content (implied reference: %s\n", p); |
CSTritt | 0:f106e3c0c589 | 22 | |
CSTritt | 1:bdea7966ea78 | 23 | // Here's another way to initialize a string. This is actually the |
CSTritt | 1:bdea7966ea78 | 24 | // approach recommended by Kieras at Michigan as it may avoid the creation |
CSTritt | 2:7d14b75d8a68 | 25 | // and storage of a literal followed by copying to the array... |
CSTritt | 1:bdea7966ea78 | 26 | char* pMyOtherString = "Here is more text."; |
CSTritt | 1:bdea7966ea78 | 27 | printf("Other string: %s\n", pMyOtherString); |
CSTritt | 1:bdea7966ea78 | 28 | |
CSTritt | 1:bdea7966ea78 | 29 | printf("Done. I sleep now.\n"); |
CSTritt | 1:bdea7966ea78 | 30 | while(true) wait(3600.0f); |
CSTritt | 0:f106e3c0c589 | 31 | } |