Declaration and Memory Representation of Array

Declaration and Memory Representation of Array

Published by: Nuru

Published date: 21 Jun 2021

Declaration and Memory Representation of Array Photo

Declaration and Memory Representation of Array

The declaration and Memory Representation of the Array refers to the number of elements and portray the memory bits in the specific location respectively.

Declaration of One dimensional Array

The value of a single subscripted index from 0 to n-1 refers to the individual elements. Here, n is the size of the array. For example, the declaration int a [4]; is a 1-Dimensional array of the integer data type. It has four elements in it. They are: a [0], a [1], a [2], a [3]. But before using an array it is important that it should be declared.

The general form of a single-dimensional array is:

Syntax:

storage_class   data_type   array_name [size]

  • The storage_class is the storage class of the array.
  • data­_type is the data type of array. It may be like int, float, char,…, etc.
  • array_name is the name of the array.
  • size is the number of elements in the array. It is mentioned in the square bracket.

Examples:

int a [10]; a is the array of 10 integers.

float x [20]; x is an array of 20 float numbers.

char name [10]; the name is a character array of size 10. It can store 10 characters.

We should notice that the array name should not be separated from the square brackets having an index. The number of array elements must remain constant while declaring an array.

Declaration of Multidimensional array

Syntax:

storage_class  data_type  array_name [dim 1] [dim 2] [dim 3]……..[dim N];

Here, storage_class, data_type, array_name are as same as in the single dimensional array. dim 1, dim 2, dim 3,….. dim N is the positive-valued integer expressions associated with each subscript.

Thus, the total no. of elements = dim 1*dim 2*dim 3*…..*dim N

E.g. int survey [3] [5] [12];

Here, the survey is a 3-D array that can contain 3*5*12 = 180 integer type data.

For Example:

int a [3] [4]; It is a two-dimensional array with 3-rows and 4-columns

int [4] [5] [3]; It is a three-dimensional array with dimensions 4, 5, and 3.

 

Memory Representation of Array

Arrays are represented with diagrams that represent their memory use on the computer. These are represented by the square boxes that represent every bit of the memory. We can write the value of the element inside the box. Below is the figure that represents the memory representation in an array. Each box represents the amount the memory needed to hold one array of elements. The pointers in it hold the memory address of the other data. They are represented by a black disk with an arrow to the referring data.
Let us write code and represent the memory of it.

int a [5];

. . . . .

a[0] =1;

for (int i = 1; i< 5; i++)

{

a[i] = a[i-1]* 2;

}

In the example above, ‘a’ is the pointer to the memory for all of its elements. Now, in the computer memory, there is an allocation of the data bit like follows:

Declaration and Memory representation of array