Let's say we want to store some value inside a variable so it will take some memory that depend on the data type. If the data type is integer (int
) it will take 4 bytes if its character (char
) it will take 1 byte like that.
Now let's say I want to store multiple values so I need to use multiple variables. But choosing array we can use single variable to store multiple interrelated values of same data type.
An array is a linear data structure that holds values of same data type where elements are stored in contiguous memory location and can be accessed using index values starting from zero.
Elements in array can be accessed using index value mainly start from zero. An array can be accessed -
array[index_value];
If an array index value starts from 0 then the last index value will be size_of_array - 1.
It uses a random access method so we can access any data at constant time i.e., O(1).
An array is a fixed size, it cannot be changed at runtime.
There are three types of array -
One dimensional array
Two dimensional array
Multidimensional array
Declaration of Array
we initialize the array along with its declaration. We use an initializer list to initialize multiple elements of the array. An initializer list is the list of values enclosed within braces { } separated b a comma.
An area can be declared in following way -
data_type variable_name[size_of_array] = {value1, value2, . . . value n};
We can also declare an array without specifying the size at the time of declaration -
data_type variable_name[] = {value1, value2, . . . value n};
Initialization
An array can be initialised in two ways -
- At compile time: Initialisation of array statically at the time of program creation.
int arr[5];
arr = {10,20,30,40,50};
int arr[5] = {10,20,30,40,50};
int arr[5] = {10,20,30, 40, 50, 60}; // Invalid - Cannot exceed the size of array
int arr[5] = {10,20,30}; // for remaining space the default int value will be initialized
int arr[5] = {10,20,30,40.6,'a'}; // Invalid - Elements must be of same type
- At runtime: Ask the user for the elements of array.
Memory representation
As we know that array stores the values in the contiguous memory. The space in memory required for each value depend on the data type of the array let's say if the array is of integer data type then each value will take 4 bytes of space in memory for the first element and for the second element next 4 bytes and so on.
- Address of an element can be calculated by -
Base Address + (index value * size of data type)
Disadvantage
Fixed size of array - Once there is declared at compile time cannot be changed at runtime.
Wastage of memory - If we initialise the size of an array in advance but we don't know how many elements we need to specify so it might be possible that we run out of memory or it might be possible that a lot of space remain vacant.
Conclusion
Arrays in C/C++/Java efficiently store multiple values of the same data type in contiguous memory, accessed by index. Fixed size poses challenges, leading to potential memory wastage.