Firstly, there are no 3D arrays in C++. There are only one-dimensional arrays. You use the [] operator to define the number of elements:
This is an array of 3 elements. Each element is an int.
This is an array of 3 elements. Each element is an array of 4 int. Now some folks may call this a 2D array but it really is a one-dimensional array where the elements are arrays.
Therefore:
is an array of 3 elements. Each element is and array of 4 elements each of which is an array of 5 ints.
From this you can deduce the following rules:
1) For a stack array you must specify the number of elements at compile time.
2) For a dynamic array you may specify the number of elements at run time.
3) The name of the array is the address of element 0.
-
int array[3];
-
int* ptr = array; //OK. Element 0 is an int. Hence, "array" is the address of an int
-
int array1[x]; //ERROR. Number o elements no specified.
-
-
int array[3][4];
-
int (*ptr)[4] = array; //OK. Element 0 is an array of 4 int. Hence "array" is the address of an array of 4 int.
-
int** ptr1 = array; //ERROR. Element 0 is not the address of hte address of a single int.
-
-
int array[3][4][5];
-
int (*ptr)[4][5] = array; //OK. Element 0 is an array of 4 arrays of 5 ints. Hence "array" is the address of an array of 4 elements each of which are arrays of 5 int.
-
int*** ptr1 = array; //ERROR. Element 0 is not the address of ther address of the address of a single int.
-
Dynamic allcoations require yout specify the number of elements at run time.
Remember, the number of elments is the first index:
-
int num;
-
cin >> num;
-
int* array = new int[num]; //OK
-
int (*ptr)[4] = new int[num][4]; //OK
-
int (*ptr)[4][5] = new int[num][4][5]; //OK
-