Syntax for array declaration:
Datatype array_name [size];
Now consider the following declaration….
int array[3];
Here, the compiler allocates total 6 bytes or 12 bytes(depends on OS ) of continuous memory locations with single name ‘array’.
But allows to store three different integer values (each in 2/4 bytes of memory) at a time. And memory is organized as….
That means all these three memory locations are named as ‘a’.
But “how can we refer individual elements?” is the big question.
Answer for this question is, compiler not only allocates memory, but also assigns a numerical value to each individual element of an array.
This numerical value is called as “Index”.
Index values for the above example are as follows….
The individual elements of an array are identified using the combination of ‘name’ and ‘index’ as follows….
Syntax:
ArrayName [indexValue];
For the above example the individual elements can be referred to as follows…
int array[3];
If I want to assign a value to any of these memory locations (array elements), we can assign as follows….
arr[1] = 100;
The result will be as….
Storage Representation :
int array[n];
Int a[5];
The number n of elements is called the length or size of the array
Length or the no. Of data elements of the array can be obtained from the index set by
Length= UB-LB+1 = 4-0+1=5
Where UB is the largest index called Upper bound & LB is the smallest index called lower bound of the arrays.
Let LA be a linear array in the memory of computer....
loc(LA[k])=Address of the element of the array LA
Example:
loc(LA[112])=?
Computer Memory for char array
The element of LA are stored in the successive memory cells.
Computer does not need to keep track of the address of every element of LA, but need to track only the address of the first element of the array denoted by “Base(LA)” called the base address of LA.
loc(LA(k))=Base(LA)+W(K-LB)
Where W is the no. Of words per memory cell of the array LA
[W is the size of data type]
Example:
Find the address for LA[6] Each element of the array occupy 1byte
0 Comments