Source of ptr_char.cpp


  1: // Filename: PTR_CHAR.CPP
  2: // Purpose:  Compares text display methods, including
  3: //           dynamic storage with char*.

  5: #include <iostream>
  6: #include <string>
  7: using namespace std;

  9: #include "PAUSE.H"

 11: int main()
 12: {
 13:     cout << "\nThis program displays several lines "   // This is just
 14:          << "of text in various ways and you should "  // "old-fashioned"
 15:          << "\nstudy each of those ways to determine " // output of string
 16:          << "the similarities and differences. \n";    // constants.
 17:     Pause(0);


 20:     string text1[4] = { // text1 is an array of C++ string objects.
 21:         "\nThis program displays several lines ",
 22:         "of text in various ways and you should ",
 23:         "study each of those ways to determine ",
 24:         "the similarities and differences. " };
 25:     for (int i = 0; i < 4; i++)
 26:     {
 27:         cout << text1[i];
 28:         if (i%2 == 1) cout << endl;
 29:     }
 30:     Pause(0);


 33:     typedef char String80[81];
 34:     String80 text2[4] = { // text2 is an array of C-strings.
 35:         "\nThis program displays several lines ",
 36:         "of text in various ways and you should ",
 37:         "study each of those ways to determine ",
 38:         "the similarities and differences. " };
 39:     for (i = 0; i < 4; i++)
 40:     {
 41:         cout << text2[i];
 42:         if (i%2 == 1) cout << endl;
 43:     }
 44:     Pause(0);


 47:     char text3[4][81] = { // text3 is a two-dimensional array of char.
 48:         "\nThis program displays several lines ",
 49:         "of text in various ways and you should ",
 50:         "study each of those ways to determine ",
 51:         "the similarities and differences. " };
 52:     for (i = 0; i < 4; i++)
 53:     {
 54:         cout << text3[i];
 55:         if (i%2 == 1) cout << endl;
 56:     }
 57:     Pause(0);


 60:     char* text4[4] = { // text4 is an array of "pointer to char".
 61:         "\nThis program displays several lines ",
 62:         "of text in various ways and you should ",
 63:         "study each of those ways to determine ",
 64:         "the similarities and differences. " };
 65:     for (i = 0; i < 4; i++)
 66:     {
 67:         cout << text4[i];
 68:         if (i%2 == 1) cout << endl;
 69:     }
 70:     Pause(0);

 72:     return 0;
 73: }