What is Array
What is Array
Arrays are defined as the collection of similar types of data items stored at
contiguous memory locations. It is one of the simplest data structures where
each data element can be randomly accessed by using its index number.
In C programming, they are the derived data types that can store the primitive
type of data such as int, char, double, float, etc. For example, if we want to
store the marks of a student in 6 subjects, then we don't need to define a
different variable for the marks in different subjects. Instead, we can define an
array that can store the marks in each subject at the contiguous memory
locations.
Let's make this concrete with an example. Say we have an array of 5 integers
declared like this: int numbers[5] = {10, 25, 8, 33, 19};
The elements are all ints (4 bytes each on a typical machine) and live together
in a continuous chunk of 20 bytes of memory. We can access any element
directly using its index - numbers[0] is 10, numbers[3] is 33, etc.
There are some of the properties of an array that are listed as follows -
o Each element in an array is of the same data type and carries the same
size of 4 bytes.
o Elements in the array are stored at contiguous memory locations, and the
first element is stored at the smallest memory location.
o 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 data element's size.
o Fixed Size - The size of an array is fixed and cannot be changed after it
is created. You decide how many elements it can hold during the
declaration.
For example, Declaring an array of 10 integers will always hold exactly 10
integers. No more, no less.
o Index-based Access - Each element in the array is accessed using a
numerical index. The indices usually start from 0.
Like this: myArray[0], myArray[1], myArray[2] and so on.
Representation of an array
o The first thing to note is those square brackets that come after the name.
That's how you indicate an array in code. The number inside indicates
the capacity of this array. In this case, it's a 10-element array.
o Here's a catch. Each element in the array is assigned an index value. This
is how you identify and access elements. Here's the catch. The indices
start from 0, not 1, as you might expect.
o So the initial element is at index 0, the second at index 1, and so forth
until you reach the element at index 9 for this array, with 10 elements. It
may seem a bit odd. Just go along with these zero-based indices.
o When it comes to accessing elements, the syntax is quite simple. Just use
the array name followed by the index number of the element you want
within those brackets. For example, arrayName[indexNumber].
o Another intriguing aspect to highlight. All its elements are arranged
sequentially in a block when you allocate memory for an array.
o It feels akin to a group of soldiers standing in formation. This condensed
setup contributes to the efficiency of arrays when performing tasks.
Array Representation
Arrays are represented as a collection of buckets where each bucket stores one
element. These buckets are indexed from '0' to 'n-1', where n is the size of that
particular array. For example, an array with size 10 will have buckets indexed
from 0 to 9.
The basic operations in the Arrays are insertion, deletion, searching, display,
traverse, and update. These operations are usually performed to either modify
the data in the array or to report the status of the array.