|
-
August 20th, 2007, 08:59 AM
#1
multi dimensional arrays
What is the difference between multi dimensional arrays and array of arrays.
-
August 20th, 2007, 09:18 AM
#2
Re: multi dimensional arrays
Code:
#include <iostream>
using namespace std;
struct main_array
{
int a[20];
};
int main()
{
//aray of array
main_array m[20];
std::cout<<"Address of first element => "<<(long)&m[0]<<std::endl;
std::cout<<"Size of array => "<<(long)sizeof(m)<<std::endl;
std::cout<<"Address of m[10].a[10] => "<<(long)&m[10].a[10]<<std::endl;
//multidimensional array
int two_d_array[20][20];
std::cout<<"Address of first element => "<<(long)&two_d_array[0]<<std::endl;
std::cout<<"Size of array => "<<(long)sizeof(two_d_array)<<std::endl;
std::cout<<"Address of two_d_array[10][10] => "<<(long)&two_d_array[10][10];
return (1);
}
output
Code:
Address of first element => 7862192
Size of array => 1600
Address of m[10].a[10] => 7863032
Address of first element => 7860592
Size of array => 1600
Address of two_d_array[10][10] => 7861432
Press ENTER to continue.
I think you are talking about this.
Form this point of view their is not change. What i think is the only change is the memory organization of array.
In Multidimensional array all the elements are continuous
i.e if you have array int two_d_array[20][20] . If it stores in Row_major order then
address of element two_d_array[10][10] = Base address +size(no. of rows skipped + no. of column skipped)
But In array of array memory is not continuously assign.
Every row of array is stored at different memory location
Last edited by nitin1979; August 20th, 2007 at 09:32 AM.
-
August 21st, 2007, 08:42 PM
#3
Re: multi dimensional arrays
There is no difference. It's all a matter of perspective. Multidimensional arrays are represented as arrays of arrays so something like:
int array[5][20];
can be thought of as a single array containing 5 elements. Each element is simply another array (of 20 integers). Thus, when you do something like this:
int value = array[3][7]
you can think of it as indexing into two arrays like so:
The expression "array[3]" is first evaluated and it returns the element at index 3. The element returned is therefore an array of 20 integers since we already established that this is what each element of the 5 element array stores.
The subcript "[7]" is then applied to the array returned just above. Since that array is just an array of 20 integers, you get the integer at index 7.
This is conceptually difficult to digest at first (for most people) but if you draw things out on paper you'll better understand it (after some practice of course). To help with this, I suggest you consider the notion that an array such as:
int array[5];
and
int array[5][20];
are conceptually no different from the perspective that each stores 5 elements. The only difference is what each element actually is. For the first, the element is an integer so when you index into the array you get back an integer. For the second, each element is an array so when you index into it you get back an array instead (of 20 integers). Since it is an array however you can therefore index into it again which is why you see the subscript operator twice in the first example above (giving the appearance and effective behaviour of a 2D integer array)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|