Array can be of different dimentions like
a b c d -> 1 dimentional
or
| a b | -> 2 dimentional
| c d |
or
| a b c|
| d e f | -> 3 dimentional
| g h i |
Array can be of different dimentions like
a b c d -> 1 dimentional
or
| a b | -> 2 dimentional
| c d |
or
| a b c|
| d e f | -> 3 dimentional
| g h i |
Yes, Savage, you are right.
In the above example, there are 2 2D arrays.
You can also have arrays with 4 or more dimensions. But you better not use them, because it's hard to visualise how they work.
In most cases a 3D array is hard, so you better stick with 1D or 2D arrays.
You can see an array as a list of elements. If you just have 1 element (for example an integer), you can create it as follow:
Code:
int a = 5;
int b = 6;
int c = 12045;
In some cases you don't want a new variable for each element (number), so you create an array of integers. This is just a list of integers:
Code:
int d[3] = {5, 6, 12045};
In this example, you create a 1D array (because you specify just one size of the array) which contains 3 elements.
To create a 2D or more dimensional array, you have to specify more sizes of the array. A 2D array can best be visualized as a table, with rows and columns:
Code:
int e[4][3];
You can look at this array as it's a table with 4 rows and 3 columns.
3D arrays are like cubes, and need 3 sizes:
Code:
int f[3][4][5]
When you want to create a more dimensional array, you need to specify more size. But it's hard to visualize such a array.
The dimension of an array is the number of elements in the array.
Read this:
Originally posted by weaknessforcats
First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
[code=c]
int array[5];
[/code]
That is an array of 5 elements each of which is an int.
[code=c]
int array[];
[/code]
won't compile. You need to declare the number of elements.
Second, this array:
[code=c]
int array[5][10];
[/code]
is still an array of 5 elements. Each element is an array of 10 int.
[code=c]
int array[5][10][15];
[/code]
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.
[code=c]
int array[][10];
[/code]
won't compile. You need to declare the number of elements.
Third, the name of an array is the address of element 0
[code=c]
int array[5];
[/code]
Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.
[code=c]
int array[5][10];
[/code]
Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
[code=c]
int array[5][10];
int (*ptr)[10] = array;
[/code]
Fourth, when the number of elements is not known at compile time, you create the array dynamically:
[code=c]
int* array = new int[value];
int (*ptr)[10] = new int[value][10];
int (*ptr)[10][15] = new int[value][10][15];
[/code]
In each case value is the number of elements. Any other brackets only describe the elements.
Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:
[code=c]
int** ptr = new int[value][10]; //ERROR
[/code]
new returns the address of an array of 10 int and that isn't the same as an int**.
Likewise:
[code=c]
int*** ptr = new int[value][10][15]; //ERROR
[/code]
new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.
With the above in mind this array:
[code=cpp]
int array[10] = {0,1,2,3,4,5,6, 7,8,9};
[/code]
has a memory layout of
0 1 2 3 4 5 6 7 8 9
Wheras this array:
[code=cpp]
int array[5][2] = {0,1,2,3,4,5,6, 7,8,9};
[/code]
has a memory layout of
0 1 2 3 4 5 6 7 8 9
Kinda the same, right?
So if your disc file contains
0 1 2 3 4 5 6 7 8 9
Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.
Therefore, when you do your read use the address of array[0][0] and read as though you have a
one-dimensional array and the values will be in the correct locations.
Comment