1: // Filename: PTR_EX4.CPP
2: // Purpose: Illustrates pointers in the context of arrays of structs.
4: #include <iostream>
5: using namespace std;
7: int main()
8: {
9: cout << "\nThis program illustrates pointers with arrays of structs."
10: << "\nStudy the source code and the output simultaneously.\n\n";
12: // Define a BankAccount struct:
13: struct BankAccount
14: {
15: int idNum;
16: char kind;
17: double balance;
18: };
20: // Declare and initialize an array of three bank accounts:
21: BankAccount account[3] = { {123, 'C', 199.99},
22: {456, 'S', 998.98},
23: {789, 'C', 321.45} };
25: // Display the information in the three bank accounts via
26: // the "usual" array component access using the array index:
27: cout << endl;
28: for (int j = 0; j < 3; j++)
29: {
30: cout << account[j].idNum << " ";
31: cout << account[j].kind << " ";
32: cout << account[j].balance << endl;
33: }
35: // Declare a BankAccount pointer which points to
36: // the array of bank accounts:
37: BankAccount* baPtr = account;
39: // Display the information in the three bank accounts via
40: // a BankAccount pointer which is incremented to point to
41: // the next bank account after the current one has been displayed:
42: cout << endl;
43: for (int i = 0; i < 3; i++)
44: {
45: cout << baPtr->idNum << " ";
46: cout << baPtr->kind << " ";
47: cout << baPtr->balance << endl;
48: baPtr++;
49: }
51: cout << endl;
53: return 0;
54: }