An array is a collection of elements identified by index or key, where each element is stored in contiguous memory locations. Arrays provide efficient access to elements using their index and are commonly used in programming for storing collections of data.
Arrays are ideal for scenarios where the size of the data structure is known in advance and where quick access to elements by index is needed.
Think of an array as a row of mailboxes, where each mailbox has a specific number (index). Each mailbox holds a piece of mail (data) and you can quickly access any mailbox if you know its number.
Consider an array that stores the following integers: 10, 20, 30, 40, and 50. The array is structured as follows:
[10, 20, 30, 40, 50]
Each number is stored at a specific index, with 10 at index 0, 20 at index 1, and so on.
int arr[] = {10, 20, 30, 40, 50};
int[] arr = {10, 20, 30, 40, 50};
arr = [10, 20, 30, 40, 50]
int arr[] = {10, 20, 30, 40, 50};