Page 1
STORAGE STRUCTURE OF ARRAYS
> Arrays are linear data structure that store homogenous elements in contiguous memory
locations.
> Every memory location has an address. The address is basically an integer.
> If more than one memory locations are required to store a value of a given data type (say x
number of locations), then each element of the array will be stored at x locations apart
from its predecessor and its successor.
> Consider an array of five characters where each character takes one byte storage (Which is
a single memory location size in most architectures).
656 657 658 659 660 661
Here, ‘A,,,R,,,R,,,A,,,Y,,,S’ are the elements and the number under each is the memory
location. Since each character takes a single byte, each elements are stored one element apart.
> Consider another array of five integers where each element takes four bytes storage.
58 -65 44 22 44 785
569 573 577 581 585 589
Here, each element is four bytes (hence four memory locations) apart.
> In order to find the address of each elements, we can use the following code snippet -
int arr[n],i; //n is the size of array
for(i=0;i<n;i++)
printf(“%d\t”,&arr[i]);
2D array
> A 2-dimensional array is a collection of homogenous elements arranged in m rows and n
columns.
> Since the actual memory is sequential, the array elements are stored sequentially in
memory as shown in the figure below.
Page 2
STORAGE STRUCTURE OF ARRAYS
> Arrays are linear data structure that store homogenous elements in contiguous memory
locations.
> Every memory location has an address. The address is basically an integer.
> If more than one memory locations are required to store a value of a given data type (say x
number of locations), then each element of the array will be stored at x locations apart
from its predecessor and its successor.
> Consider an array of five characters where each character takes one byte storage (Which is
a single memory location size in most architectures).
656 657 658 659 660 661
Here, ‘A,,,R,,,R,,,A,,,Y,,,S’ are the elements and the number under each is the memory
location. Since each character takes a single byte, each elements are stored one element apart.
> Consider another array of five integers where each element takes four bytes storage.
58 -65 44 22 44 785
569 573 577 581 585 589
Here, each element is four bytes (hence four memory locations) apart.
> In order to find the address of each elements, we can use the following code snippet -
int arr[n],i; //n is the size of array
for(i=0;i<n;i++)
printf(“%d\t”,&arr[i]);
2D array
> A 2-dimensional array is a collection of homogenous elements arranged in m rows and n
columns.
> Since the actual memory is sequential, the array elements are stored sequentially in
memory as shown in the figure below.
int arr[4][4]={
{56, 85, 82, 66},
{20, 56, 125, 785},
{96, 75, 21, 5},
{785, 45, 223, 451},
};
56 85 82 66 20 56 125 785 96 75 21 5 785 45 223 451
600 604 608 612 616 620 624 628 632 636 640 644 648 652 6 5 6 6 6 0
> Since it is an integer array, each element took four bytes of storage. So the memory
location of each is separated by four units as well.
> The above method of storing is called row-major order. Here, the values are stored row
wise i.e. all elements of zeroth row are stored first, followed by that of first row and so on.
> The address of (i,j)th cell can be found by
Base address + (i*n) + j //m X n matrix
> Similarly in column major order, elements are stored column wise.
> C programming language uses row-major order.
Multi-dimensional array
> A multi-dimensional array has more than two dimensions.
> Consider a three by m by n array. It is basically a collection of three two dimensional
array.
> Hence, to store it, the first two dimensional array is stored as mentioned before, followed
by second 2D array followed by 3 2D array.
Read More