1: //pointers1.cpp
3: #include <iostream>
4: #include <string>
5: #include <iomanip>
6: using namespace std;
8: int main()
9: {
10: cout << "\nThis program illustrates pointer syntax and displays the "
11: "values of both\nuninitialized and initialized pointer variables.";
12: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
14: int* p_int; //p_int is a "pointer to an int".
15: //p_int can contain the storage location of an int.
17: char* p_char; //p_char is a "pointer to a char".
18: //p_char can contain the storage location of a char.
20: struct ClubMember
21: {
22: string name;
23: int age;
24: double balance;
25: };
26: ClubMember* p_cm; //p_cm is a "pointer to a ClubMember".
27: //p_cm can contain the storage location of a ClubMember.
29: //Declare and initialize a pointer-to-integer variable
30: int i1 = 6;
31: int* p_int1 = &i1;
33: //Declare a pointer-to-char variable, then assign it a value
34: char c1 = 'T';
35: char* p_char1;
36: p_char1 = &c1;
38: //Another declaration with initialization,
39: //this time for a pointer-to-struct variable
40: ClubMember cm1 = { "John", 32, 100.00 };
41: ClubMember* p_cm1 = &cm1;
43: //This section may cause a run-time error, and the corresponding
44: //variables declared above should cause compiler warnings.
45: //cout << "\nHere are some values of uninitialized pointer variables:";
46: //cout << "\nValue of p_int: " << p_int;
47: //cout << "\nValue of p_char: " << p_char;
48: //cout << "\nValue of p_cm: " << p_cm;
49: //cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
51: cout << "\nInteger variable i1 is stored at this "
52: "memory location: " << &i1;
53: cout << "\nAnd here is the value of pointer variable p_int1, "
54: "which points at i1: " << p_int1;
55: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
57: cout << "\nCharacter variable c1 is stored at this "
58: "memory location: " << hex << &c1;
59: cout << "\nAnd here is the value of pointer variable p_char1, "
60: "which points at c1: " << p_char1;
61: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
63: cout << "\nStruct variable cm1 is stored at this "
64: "memory location: " << &cm1;
65: cout << "\nAnd here is the value of pointer variable p_cm1, "
66: "which points at cm1: " << p_cm1;
67: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
68: }