Charles Tritt
/
PointerPlay
Demonstrate basic pointer use.
Diff: main.cpp
- Revision:
- 0:f4431aea05f2
- Child:
- 1:66b36106613a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Nov 03 02:19:44 2017 +0000 @@ -0,0 +1,27 @@ +/* + Project: PointerPlay + File: main.cpp + + Demonstrates some uses of pointers. + + Written by: Dr. C. S. Tritt + Created: 9/24/17 (v. 1.0) + +*/ +#include "mbed.h" + +int main() { + printf("\nPointer Play -- \"a is a.\"\n"); + // Each set of lines below create a variable and a pointter to it. + float myNum1 = 1.23f; // Initialize the variable. + float* pNum1 = &myNum1; // Initialize the pointer to point to it. + printf("myNum1 is %f and *pNum1 is %f.\n", myNum1, *pNum1); + int myNum2; // Create a variable. + int* pNum2 = NULL; // Create a NULL (unassigned) pointer. + pNum2 = &myNum2; // Put the address in the pointer. + *pNum2 = 15; // Dereference the pointer to store a value. + printf("myNum2 is %d and *pNum2 is %d.\n", myNum2, *pNum2); + // Examine the addresses (pointer values). + printf("pNum1 is %p and pNum2 is %p.\n", pNum1, pNum2); + while(true); // Wait here forever. +} \ No newline at end of file