2. Array
● An array is defined as the collection of similar type of
data items stored at contiguous memory locations.
● The array is the simplest data structure where each
data element can be randomly accessed by using its
index number.
● Each element of an array is of same data type and
carries the same size, i.e., int = 4 bytes.
3. ● Elements of the array are stored at contiguous memory
locations where the first element is stored at the smallest
memory location.
● Elements of the array can be randomly accessed since we
can calculate the address of each element of the array
with the given base address and the size of the data
element.
4. Array Declaration
● Syntax:
○ dataType arrayName[arraySize];
○ int mark[5];
● Size is 5 - It can hold 5 integer values.
mark[0] mark[1] mark[2] mark[3] mark[4]
0 1 2 3 4
Indexes
First Element Last Element
5. Array Initialization
Example:
● int mark[5] = {19, 10, 8, 17, 9};
OR
● int mark[] = {19, 10, 8, 17, 9};
● No need to specify the size. The compiler knows its size is 5 as we are initializing it
with 5 elements.
6. Multidimensional Arrays
2D Array
● Syntax:
○ dataType arrayName[numberOfRows] [numberOfColumns];
○ Example:
○ int marks [2][3];
○ Can Store 6 values.
0 1 2
0 mark[0][0] mark[0][1] mark[0][2]
1 mark[1][0] mark[1][1] mark[1][2]
Column Indexes
Row
Indexes
7. 2D Array Initialization
Example:
● int mark[2][3] = { {75,65,96} , {34,72,56} };
OR
● int mark[][3] = { {75,65,96} , {34,72,56} };
○ Need to specify the number of columns. The number of rows can be inferred
from the initializer list.
8. Advantages of C Array
● Code Optimization : Less code to the access the data.
● Ease of traversing : By using the for loop, we can retrieve the
elements of an array easily.
● Random Access : We can access any element randomly using the
array.
9. Disadvantages of C Array
● Fixed Size:
○ Arrays have a fixed size, the number of elements is specified when they are declared.
● Limited Data Types:
○ Arrays usually store elements of a single data type. If you need to store different data
types in a collection, you would either need to create separate arrays for each data type
or use a more complex data structure.
● Insertion and Deletion:
○ Inserting or deleting elements in an array can be inefficient and time-consuming,
especially if the array is large or if the insertion/deletion occurs at the beginning or
middle of the array.