HOWTO: Create Two-Dimensional Arrays with Operator new

Last reviewed: September 30, 1997
Article ID: Q102391

The information in this article applies to:
  • Microsoft C/C++ for MS-DOS, version 7.0
  • Microsoft Visual C++ for Windows, versions 1.0, 1.5
  • Microsoft Visual C++, 32-bit Edition, versions 1.0, 2.0, 2.1, 4.0, 5.0

SUMMARY

The code example below demonstrates creating a two-dimensional array using the C++ operator new. For more information on creating a two- dimensional array in C with the malloc() function, please search in the Microsoft Knowledge Base on the following words:

   dynamic memory allocation two dimensional array

MORE INFORMATION

To conceptualize a two-dimensional array, consider a one-dimensional array of one-dimensional arrays. To construct a two-dimensional array, first allocate an array of pointers to pointers. Then, index through the first array and allocate additional one-dimensional arrays.

The first code example below copies command-line arguments into a two- dimensional array. The first dimension represents the total number of command-line arguments. The second dimension has a variable length and represents the length of each command-line argument.

The second code example below dynamically creates a two-dimensional array of integers where user input specifies the size of each dimension. The example also initialized the array to contain random values and displays the contents of the array.

Sample Code 1

   /*
    * Compiler options needed: None
    */

   #include <iostream.h>
   #include <new.h>
   #include <string.h>

   char **TwoDArray;

   void main(int argc, char **argv)
   {
      // Allocate the first dimension.
      TwoDArray = new char*[argc];

      for (int i = 0; i < argc; i++)
         {
         // Allocate the second dimension
         TwoDArray[i] = new char[strlen(argv[i]) + 1];
         strcpy(TwoDArray[i], argv[i]);
         }

      cout << "This is the command line that was passed in:" << endl;
      for (i = 0; i < argc; i++)
         {
         cout << "argv[" << i << "] = ";

         // Print the command line arguments one at a time.
         for (unsigned j = 0; j <= strlen(TwoDArray[i]); j++)
            cout << TwoDArray[i][j];
         cout << endl;
         }

      // Delete the 2D array
      for (i = 0; i < argc; i++)
         delete [] TwoDArray[i];
      delete [] TwoDArray;
   }

Sample Code 2

   /*
    * Compiler options needed: None
    */

   #include <iostream.h>
   #include <new.h>
   #include <stdlib.h>
   #include <time.h>

   int **MyArray;

   void main(void)
   {
      int d1, d2, ndx1, ndx2;

      cout << "Enter the first array dimension: ";
      cin >> d1;
      cout << "Enter the second array dimension: ";
      cin >> d2;

      // Allocate memory for the array
      MyArray = new int*[d1];
      for (ndx1 = 0; ndx1 < d1; ndx1++)
         MyArray[ndx1] = new int[d2];

      // Fill the array with random integer values between 0 and 9
      srand((unsigned)time(NULL));
      for (ndx1 = 0; ndx1 < d1; ndx1++)
         for (ndx2 = 0; ndx2 < d2; ndx2++)
             MyArray[ndx1][ndx2] = rand() % 10;

      // Display the array
      for (ndx2 = 0; ndx2 < d2; ndx2++)
         cout << '\t' << "col " << ndx2;   // col #'s
      cout << endl;

      for (ndx1 = 0; ndx1 < d1; ndx1++)
         {
         cout << "row " << ndx1;           // row #'s

         for (ndx2 = 0; ndx2 < d2; ndx2++)
            {
            cout << '\t';
            cout << MyArray[ndx1][ndx2];
            }
         cout << endl;
         }

      // Delete the array
      for (ndx1 = 0; ndx1 < d1; ndx1++)
         delete [] MyArray[ndx1];
      delete [] MyArray;
   }


Additional query words: 8.00 8.00c
Keywords : CPPLangIss kbcode kbfasttip
Version : WINDOWS:1.0,1.5; WINDOWS NT:1.0,2.0,2.1,4.0
Platform : NT WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 30, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.