chevron_left
1,680 points
10 5 2

The 'base address' of a 2D array is the starting point of the array, it may be the given address to calculate the element’s address of a 2D array. An array is a data structure that is used to store big data for processing. Multiple data processing operations can be applied to an array. In the article, we will look at the details of the 2D array and the process of calculation of an element’s address with the help of ‘the base address of a two dimensional array’.

Photo by Tatiana Syrikova

What is an Array?

A data structure with a collection of elements is called an array. These components are typical of all the same data types, like an integer or string. Computer applications frequently utilize arrays to organize data so that a related set of values may be quickly searched for or sorted.

Known types of the arrays.

Single-dimensional arrays, two-dimensional arrays, and multidimensional arrays are the three different types or kinds of arrays.

Single-dimensional array.

An example of a linear array is a single dimension array (also known as a one-dimensional array). Its components are accessed via a single subscript, which can either be a row or column index. Here I will take the example of C language declaration

int anArrayName[10];

as an illustration, which specifies a single-dimensional array of ten numbers. A single-dimensional array is a structured group of elements that may be accessed separately by designating a single index value for each element's position. The single-dimensional array is also called an array of elements.

Two-dimensional array.

You can think of a two-dimensional array as a matrix with rows and columns. A two-dimensional array can be declared using the following general syntax:

int myArray[10][10]; // C language declaration example.

A collection of different one-dimensional arrays makes up a two-dimensional array. To develop a data structure that resembles a relational database, a 2D array is made. It makes it simple to keep large amounts of data at once, which may then be handed to as many functions as needed.

Multi-dimensional array.

An array of arrays that contains homogeneous data in tabular form is what is referred to as a multi-dimensional array. An array with more than one dimension (a two-dimensional array is also a multi-dimensional array) is referred to as a multi-dimensional array. It is an array of arrays and a multi-level array. The 2D or 3D array, often known as a two-dimensional or three-dimensional array, is the most basic multi-dimensional array. Technically speaking, as you will see in the code, it is an array of arrays. A 2D array is also known as a matrix, a table with rows and columns, or a 2D array.

Int myArry[10][10][10]; // //C language declaration example of a three-dimensional array

What is the base address of an array?

The base address is the starting address of the element of an array. It is used to calculate the element's address, if it is not specified to calculate the address then assume it 0. So in simple words, I can say the base address of any array is the starting address if not specified.

How do arrays stores in memory?

Understanding the storage procedure of an array in memory is simple, arrays are stored two ways one is row-major and the second one is column-major.

Row-major order.

The rows of the 2D array are continuously kept in memory using row-major ordering. The graphic depicts an array, and the memory allocation according to row-major order is displayed as follows.

For instance: We have a 2D array that has 3 rows and 3 columns.

Int myArray[3][3]; 

(0,0)

(0,1)

(0,2)

(1,0)

(1,1)

(1,2)

(2,0)

(2,1)

(2,2)

It will be stored like a lenient row in the memory.

First, the array's first row is entirely saved in memory. Then, the array's second row, and so on until the last row, are entirely stored in memory.

Column major order.

All the columns of the 2D array are continuously kept in memory following the column-major ordering. The array that is seen in the above figure has the following memory allocation:

(0,0) (1,0) (2,0) (0,1) (1,1) (2,1) (2,0) (2,1) (2,2)

It is also stored in the same way by column.

The first column of the array is fully stored in memory first, followed by the second row, the third row, and so on, up to the last column of the array.

How to calculate an element’s address with the help of the base address of a 2D array?

Two separate formulas are used to determine the address of a random element in a two-dimensional array since there are two different methods for storing the array in memory.

Employing for Row-major order.


Address of an element a[i][j] of the array stored in row-major order is determined as if the array is declared by a[m][n] where m is the number of rows and n is the number of columns.

Address(a[i][j]) = BA. + I  n + j)  size.

where BA. denotes the base address or the location of the array's first element, if not specified then assume it as a[0][0].

a[10...30, 55...75], base address of the array (BA) = 0, size of an element = 4 bytes. 

Find the location of a[15][68].

Address(a[15][68]) = 0 +   ((15 - 10) x (68 - 55 + 1) + (68 - 55)) x 4
= (5 x 14 + 13) x 4
= 83 x 4
= 332 answer

Employing column-major order.

Address of an element a[i][j] of the array stored in row-major order is determined as if the array is declared by a[m][n] where m is the number of rows and n is the number of columns.

Address(a[i][j]) = ((jm)+i)Size + BA 

where BA is the array's base address.

 A [-5 ... +20][20 ... 70], BA = 1020, Size of element = 8 bytes. Find the location of a[0][30].   
Address [A[0][30]) = ((30-20) x 24 + 5) x 8 + 1020 = 245 x 8 + 1020 = 2980 bytes

The Conclusion

I hope after reading this article anyone can understand what the array is, how many types of arrays has, what is the base address and how to calculate the address of an element with the base address.

If you read this far, tweet to the author to show them you care. Tweet a Thanks

More Posts

The truth value of an array with more than one element is ambiguous. use a.any() or a.all() AAbouelenien - Sep 22
Valueerror: setting an array element with a sequence Ankur Ranpariya - Sep 17
Write a function max that has two java string parameters and returns the larger of the two NoirHusky - Sep 2
Given an array of ints, return true if the array contains a 2 next to a 2 somewhere. NoirHusky - Sep 8
Write a loop that sets newscores to oldscores shifted once left, with element 0 copied to the end. sadmin - Apr 29
How to sort an array in java without using the sort method Ankur Ranpariya 1 - Aug 12
Failed to load resource: the server responded with a status of 404 (not found) Ankur Ranpariya - Sep 18
Failed to load resource: the server responded with a status of 400 (bad request) zhteja - Sep 17
How to get mac address of client machine in PHP James - May 31
Program to calculate number of vowels in a string in Python Ferdy - Aug 6
How to check if an array is empty in C++? AnkurRanpariyav - Aug 7
How can a call to an overloaded function be ambiguous? zhteja - Aug 10
How to filter records from the database with dropdown using PHP Codeigniter AnkurRanpariyav - Aug 5
How to link to a specific part of a page. Bushra Rubab - Sep 13
How to tell what version of PHP a site is running? NoirHusky - Sep 12
Write a Program to Print First x Terms of the Series 3n+2 which are not Multiples of 4 beekip - Sep 20
Write a 'C' program to find the day of the given date. Bushra Rubab - Sep 9
There are 4 bytes in an ipv4 address. what is the highest decimal value you can have for one byte? James - Jun 1
Given a string, return a string where for every char in the original, there are two chars. Ankur Ranpariya - Jul 19
How to give space between two buttons in HTML AnkurRanpariyav - Aug 6