Share via


を使用して配列 (C++)

配列の各要素には配列の添字演算子 () を使用してアクセス [入力]。単独で次元設定された配列の添字のない式で使用すると配列名は配列の最初の要素へのポインターになります。次に例を示します。

// using_arrays.cpp
int main() {
   char chArray[10];
   char *pch = chArray;   // Pointer to first element.
   char   ch = chArray[0];   // Value of first element.
   ch = chArray[3];   // Value of fourth element.
}

多次元配列を使用するとさまざまな組み合わせは式で使用できます。次に例を示します。

// using_arrays_2.cpp
// compile with: /EHsc /W1
#include <iostream>
using namespace std;
int main() {
   double multi[4][4][3];   // Declare the array.
   double (*p2multi)[3];
   double (*p1multi);

   cout << multi[3][2][2] << "\n";   // C4700 Use three subscripts.
   p2multi = multi[3];               // Make p2multi point to
                                     // fourth "plane" of multi.
   p1multi = multi[3][2];            // Make p1multi point to
                                     // fourth plane, third row
                                     // of multi.
}

このコードではmulti は型 倍精度浮動小数点型 の次元配列です。サイズが 3 の 倍精度浮動小数点型 型の配列への p2multi のポインターのポインター。配列は1 個2 個およびこの例では3 種類の添字を使用します。すべての添字を指定するのが一般的ですが cout のステートメントは次のステートメントのように配列要素の特定のサブセットを選択すると便利な場合があります。

参照

関連項目

配列 (C++)