What are arrays?

An array is a collection of the same type of elements that are stored in a contiguous memorycontinuous blocks of memory location.

Think of an array as a bookshelf that has several books placed on it. We can say that we have an array of books. Now, suppose we wish to read the book, The Art of Computer Programming (placed on the 5th location on the shelf). We’ll have to access the book (element) from the shelf (array).

We can do this in two ways:

  • We either fetch the fifth book (element) directly.

  • In case we don’t know which place (index) the book is, we’ll have to traverse the shelf (array) from the first index to the index on which the book is placed.

In C/C++, the indexing of arrays starts from 0 and not 1. In some languages, the array indexing starts from 1.

Get hands-on with 1200+ tech skills courses.