Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio .NET
Visual C++
Reference
C/C++ Languages
Declarators
Arrays
This page is specific to
Microsoft Visual Studio 2003/.NET Framework 1.1

Other versions are also available for the following:
C++ Language Reference
Arrays

An array is a collection of like objects. The simplest case of an array is a vector. C++ provides a convenient syntax for declaration of fixed-size arrays:

decl-specifiers  dname  [  constant-expressionopt  ]  ;

The number of elements in the array is given by the constant-expression. The first element in the array is the 0th element, and the last element is the (n-1th) element, where n is the size of the array. The constant-expression must be of an integral type and must be greater than 0. A zero-sized array is legal only when the array is the last field in a struct or union and when the Microsoft extensions (/Ze) are enabled.

The following example shows how to define an array at run time:

// arrays.cpp
// compile with: /EHsc
#include <iostream>
int main()
{
   using namespace std;
   int size = 0;
   cout << "how big should the array be? ";
   cin >> size;
   int* myarr = new int[size];
   for (int i = 0 ; i < size ; i++)
      myarr[i] = 10;

   for (i = 0 ; i < size ; i++)
      printf("myarr[%d] = %d\n", i, myarr[i]);
}

Input

3

Sample Output

myarr[0] = 10
myarr[1] = 10
myarr[2] = 10

Arrays are derived types and can therefore be constructed from any other derived or fundamental type except functions, references, and void.

Arrays constructed from other arrays are multidimensional arrays. These multidimensional arrays are specified by placing multiple [ constant-expression ] specifications in sequence. For example, consider this declaration:

int i2[5][7];

It specifies an array of type int, conceptually arranged in a two-dimensional matrix of five rows and seven columns, as shown in the following figure:

Conceptual Layout of Multidimensional Array

In declarations of multidimensioned arrays that have an initializer-list (as described in Initializers), the constant-expression that specifies the bounds for the first dimension can be omitted. For example:

// arrays2.cpp
const int cMarkets = 4;
// Declare a float that represents the transportation costs.
double TransportCosts[][cMarkets] =
{ 
  { 32.19, 47.29, 31.99, 19.11 },
  { 11.29, 22.49, 33.47, 17.29 },
  { 41.97, 22.09,  9.76, 22.55 }
};

int main()
{
}

The preceding declaration defines an array that is three rows by four columns. The rows represent factories and the columns represent markets to which the factories ship. The values are the transportation costs from the factories to the markets. The first dimension of the array is left out, but the compiler fills it in by examining the initializer.

Example

The technique of omitting the bounds specification for the first dimension of a multidimensioned array can also be used in function declarations as follows:

// multidimensional_arrays.cpp
// compile with: /EHsc
// arguments: 3

#include <limits>         // Includes DBL_MAX
#include <iostream>

const int    cMkts = 4, cFacts = 2;

// Declare a float that represents the transportation costs
double TransportCosts[][cMkts] =
{ { 32.19, 47.29, 31.99, 19.11 },
  { 11.29, 22.49, 33.47, 17.29 },
  { 41.97, 22.09,  9.76, 22.55 }  };
// Calculate size of unspecified dimension
const int cFactories = sizeof TransportCosts /
                       sizeof( double[cMkts] );

double FindMinToMkt( int Mkt, double TransportCosts[][cMkts], int cFacts );

using namespace std;

int main( int argc, char *argv[] )
{
    double MinCost;
if (argv[1] == 0) {
   cout << "You must specify the number of markets." << endl;
   exit(0);
   }
    MinCost = FindMinToMkt( *argv[1] - '0', TransportCosts, cFacts );
    cout << "The minimum cost to Market " << argv[1] << " is: "
         << MinCost << "\n";
}

double FindMinToMkt( int Mkt, double TransportCosts[][cMkts],
                     int cFacts )
{
    double MinCost = DBL_MAX;
    for( int i = 0; i < cFacts; ++i )
        MinCost = (MinCost < TransportCosts[i][Mkt]) ?
                   MinCost : TransportCosts[i][Mkt];
    return MinCost;
}

Output

The minimum cost to Market 3 is: 17.29

The function FindMinToMkt is written such that adding new factories does not require any code changes, just a recompilation.

Topics in this section:

See Also

C++ Abstract Declarators

© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker