## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required Array
1. What is an array?
Ans:
1. An array is a linear data structure.
2. It is a finite collection of data elements.
3. An array permits homogeneous data. It means its each data element is of same type.
4. Similar data type elements are stored contiguously in the memory and that too under one variable name.
2. How elements of an array are stored?
Ans: Elements of array are stored in contiguous memory locations. One-dimensional or more
than one-dimensional elements are stored contiguously. Figure 7.1 shows storage of elements of a
one-dimensional array Num contiguously.
Num
1
65516
Num
2
65518
Num
3
65520
Num
2
65522
Num
5
65524
Figure 7.1 Storage of One-Dimensional Array
Where
Num = 1 and its Address : 65516
Num = 2 and its Address : 65518
Num = 3 and its Address : 65520
Num = 2 and its Address : 65522
Num = 5 and its Address : 65524
7
M07_ITL-ESL4791_02_SE_C07.indd 130 12/22/2012 5:02:59 PM Array II-131
3. Explain the syntax of a one-dimensional array.
Ans: Declaration of a one-dimensional array can be done with data type first, followed by it is
variable name and, at last, the size is enclosed in square bracket.
The syntax of declaration of one-dimensional array is as follows:
Data-type var_name[Size];
Example:
int a;
It tells to the compiler that a is an integer type of array and can store five integers. The compiler
reserves 2 bytes of memory for each integer array element, i.e., 10 bytes are reserved for storing five
integers in the memory.
In the same way, array of different data types can be declared as follows:
char ch;
float real;
long num;
4. How are array elements accessed?
Ans: Individual elements of the array are accessed by specifying the subscript. Index is the other
name of subscript. The subscript is enclosed in the square bracket. Subscript provides an easy way to
address the individual elements of an array. Array elements are numbered starting from 0, i.e., subscript
starts from 0. The first element of the array is num, second is num and so on.
Subscript to the array speciﬁes an exact element in the array.
For example,
num=1; /* First element*/
num=2;
num=4;
num=2;
num=5;
5. How is one-dimensional array initialization done?
Ans: Assigning initial values to an array is nothing but initialization of an array. The array initializa-
tion can be done as under:
int a ={1,2,3,4,5};
where five elements are stored in an array a. The array elements are stored sequentially in separate
locations. Then, question arises how to call individually to each element from this bunch of integer ele-
ments. Reading of array elements begins from 0. By indicating the position of element, one can retrieve
any element of an array. Array elements are called with array name followed by element numbers.
Table 7.1 explains the same.
Table 7.1 Calling Array Elements
a refers to 1st element i.e. 1
a refers to 2nd element i.e. 2
a refers to 3rd element i.e. 3
a refers to 4th element i.e. 4
a refers to 5th element i.e. 5
M07_ITL-ESL4791_02_SE_C07.indd 131 12/22/2012 5:02:59 PM

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required