How 2D array is represented in memory? Explain with example

5.A) How 2D array is represented in memory? Explain with a suitable example

Answer:-

Two-Dimensional Arrays:

A list of items can be given one variable name using two subscripts and such a variable is called a two-dimensional array.
It consists of both rows and columns. Ex: Matrix.


Declaration of Two-Dimensional Array:

Here is the general syntax for array declaration along with examples.

Initialization of Two-Dimensional Array:

After array is declared, the next is storing values into an array is called initialization.

There are two types of array initialization:

  1. Compile-time initialization
  2. Run-time initialization
  1. Compile-time initialization:

If we assign values to the array during declaration it is called compile-time initialization.

Following are the different methods of compile-time initialization.

2. Run time initialization:

Run time initialization is storing values in an array when the program is running or executing.

The following example illustrates run time storing of values using scanf and for loop:

Example:

printf(“Enter the marks”);
for(i=0; i<3; i++) for(j=0;j<4;j++)
{
scanf(“ %d”, &marks[i][j]);
}

More Examples: Other way of initialization:
int a[ ][3]= { 0, 1, 2, 3,4,5,6,7,8};
int b[ ][4] ={1,2,3,4,5,6,7,8,9,10,11,12};

Example for Invalid initialization
int A[3][ ]={1,2,3};
Note: Never have column size undeclared in two dimension array.

Leave a Reply

Your email address will not be published. Required fields are marked *