198 Programming and Data Structures
b) In the statement a [2 ] =a [3] or vice versa, value of a [2 ] is assigned to a [3], where
both the elements are of the same array.
c) The array elements are stored in continous memory locations. The amount of storage
required for holding elements of the array depends on its type and size. The total size in bytes
for a single dimensional array is computed as shown below.
Total bytes = sizeof (data type) X size of array
7.5 ONE-DIMENSIONAL ARRAY
The elements of an integer array a [ 5 ] are stored in continuous memory locations. It is assumed that
the starting memory location is 2 0 0 0. Each integer element requires 2 bytes. Hence subsequent element
appears after gap of 2 locations. Table 7.1 shows the locations of elements of an integer array.
Table 7.1 Integer Data Type & Their Memory Locations
Element A[0] A[l] A [2]
A [3]
A [4]
Address 2000
2002
2004
2006
2008
Similarly, the elements of an arrays of any data type are stored in continuous memory location. The
only difference is that number of locations are different for different data types.
An example is illustrated below based on this point.
7.1 Write a program to print bytes reserved for various types of data & space required for
storing them in memory using arrays.
# Include <stdio.h>
# include <conio.h>
mainQ
{
int i [10] ;
char c[10];
long 1 [10];
clrscrO ;
printf ("The type 'int' requires %d Bytes ",sizeof( in t));
printf ("nThe type 'char'requires %d Bytes",sizeof(char));
printf ("nTlte type 'long' requires %dBytes"fsizeof(long));
printf ("
%d memory locations are reserved for ten 'int' elements"rsizeof(i));
printf ("n %d memory locations are reserved for ten 'char' elements", sizeofc));
printf ("
%d memory locations are reserved for ten 'long' elements",sizeof(l));
I
OUTPUT:
The type 'int' requires 2 Bytes
The type 'char7 requires 1 Bytes
The type 'long' requires 4 Bytes
20 memory locations are reserved for ten 'int' elements
10 memory locations are reserved for ten 'char' elements
40 memory locations are reserved for ten 'long' elements