All about Arrays in C/C++  Part II
IntroductionThis article talks about MultiDimensional Arrays in C/C++. About 1Dimensional Array see All about Arrays in C/C++  Part I BackgroundMultiDimensional Arrays Multidimensional arrays are nothing but Array of Arrays. Characteristics All the characteristics that are mentioned for 1Dimensional array hold good for multidimensional arrays too. Apart from that
Memory Mapping In C/C++, memory mapping for multidimensional arrays, is done using Row Major ordering. It means, memory is allocated for successive elements by incrementing the rightmost index until it reaches the end of the current Row. 2Dimensional Arrays A 2Dimensional Array is nothing but Array of 1Dimensional Array and is represented with two subscripts as below: data_typeOfArray arrayName [firstDimension] [secondDimension] whereas firstDimension represents Rows and secondDimension as Columns. For Example: int array[2][3]; Can be said as 2 Arrays of 1Dimensional Array of size 3; 2Dimensional array can be imagined as a 2D table made of elements of same data type as in below picture: http://www.go4expert.com/images/arti...2/Arrays4.JPG Memory Mapping in 2D Array For Example if we take int array[2][3]; Here array will be considered as base address and successive address for the elements will be allocated at sizeOfElement*offset in Row Major Ordering. And here if we take the sizeOfElement as 1 then memory mapping for array[2][3] looks as below: array[0][0] > 0 array[0][1] > 1 array[0][2] > 2 array[1][0] > 3 array[1][1] > 4 array[1][2] > 5 Where 0,1,2 etc at right hand side are offsets Size of a 2D Array Here size of the Array can be calculated as: firstDimension*secondDimension*sizeOfDataType For example: size of the int array[2][3] will be 2*3*4= 24 bytes Address of an Element Address of an Element at a given rowIndex and colIndex in an array[rowSize][colSize] can be calculated using the below formula: Element_Address = Base_Address + (col_size*(rowIndex) + (colIndex)) * Element_Size For Example: If we take the above array[2][3] with assumption of baseAddress as 0 and element size as 1, then address of element at indices array[0][1] can be found as: array[0][1] = 0 + (3*(0)+1)*1 = 0 + (0+1)*1 = 0 + 1 = 1 Initializing a 2D Array As like in 1D Arrays, 2D Arrays can be initialized as below: int arry[2][3]={10,20,30,40,50,60}; or int array[2][3] = {{10,20,30}, {40,50,60}} And they are assigned with array indices as below: array[0][0]=10; array[0][1]=20; array[0][2]=30; array[1][0]=40; array[1][1]=50; array[1][2]=60; Like in 1D array, if some of the indices in an interger 2D array are not given initilizers, then by default they will be initilized with 0. Also if we assign 2D Array with {}, it would initialize all it's elements with 0 value. 2D Array of characters Like in 1D Arrays, 2D Arrays of strings can be initilized in two ways as below: char array[3][4] = { 'd','o','g' 'c','a','t', 'r','a','t' } or char array[3][4] = {"dog", "cat", "rat"}; As said above for integer arrays, if initilizers are not given for some elements in character arrays, they will be initilized with NULL characters. Accessing elements in 2D Arrays In the above mentioned array i.e. int array[2][3], we can access the 1st Rowth 0th column element as array[1][0] i.e. 40 Dynamic Allocation of 2D Arrays Suppose we want an int array[row][col]. where size of row and col are unknown at compile time. Then we can allocate the array dynamically and access it's elements with below steps:
For Example: The codeCode: Cpp
As said above, the size of the first dimension may be omitted, but the second dimension has to be mentioned in the argument. For Example: The codeCode: Cpp
Imagine a 2D array as shown in below example picture and then put the elements at the respective indices in the picture. This helps to read/recognise any element of the array at a given indcies. http://www.go4expert.com/images/arti...2/2dArray.JPG 3Dimensional Arrays Multi Dimensional Arrays are not limited to 2D alone. They can be of as many indices as needed. But developer must be careful here, as the memory that consume increases with proportional to the dimension. A 3Dimensional Array can be said as Array of 2Dimensional Arrays and is represented with three subscripts as below: data_typeOfArray arrayName [firstDimension] [secondDimension] [thirdDimension] whereas firstDiemnsion is also called as depthSize here. For Example: int arry[2][3][4]; can be called as 2 Arrays of 2Dimensional Arrays of size 3X4 !! Memory Mapping in 3D Array If we take int array[2][3][4]; And array will be considered as base address and successive address for the elements will be allocated at sizeOfElement*offset in Row Major Ordering as below: array[0][0][0] > 0 array[0][0][1] > 1 array[0][0][2] > 2 array[0][0][3] > 3 array[0][1][0] > 4 . . . array[0][2][0] > 8 . . . array[1][0][0] > 12 . . . array[1][1][0] > 16 . . . array[1][2][0] > 20 array[1][2][1] > 21 array[1][2][2] > 22 array[1][2][3] > 23 Where 0,1,2 etc at right hand side are offsets Size of a 3D Array Here size of the Array can be calculated as firstDimension*secondDimension*thirdDimension*size OfDataType For example the size of the below array will be: int array[2][3][4] 2*3*4*4 = 96 bytes Address of an Element Address of an Element at a given depthIndex, rowIndex and colIndex in an array[depthSize][rowSize][colSize] can be calculated using the below formula: Element Address = Base Address + ((depthIndex*colSize+colIndex) * rowSize + rowIndex) * Element_Size If we take the above array[2][3][4] with assumption of baseAddress as 0 and element size as 1, then address of an element at indices array[1][2][3] can be found as: array[1][2][3] = 0 + ((1*4+3)*3+2)*1 = 0 + ((4+3)*3+2)1 = 0 + ((7)*3+2)1 = 0 + 23 Easy way of Reading a 3D Array Elements in a 3D array also can easily be read/recognised by imagining the picture of the Array as below: http://www.go4expert.com/images/arti...2/3dArray.JPG 
Re: All about Arrays in C/C++  Part II
good artical......
thnx........... 
Re: All about Arrays in C/C++  Part II
I like this's, cool!

Re: All about Arrays in C/C++  Part II
Nominate this article for Article of the month  Nov 2009

Re: All about Arrays in C/C++  Part II
well written. Such an article with examples, figures and code examples always help people to understand the concept better. Thanks for writing and sharing.

Re: All about Arrays in C/C++  Part II
Right on, write on about whatever you feel best.

All times are GMT +5.5. The time now is 09:23. 