Overview of Arrays In C
What is array in C
An array is a collection of elements of the same data type stored in contiguous memory locations.
An array is declared with a specified data type, a name, and a size. The size of an array determines the number of elements that the array can store.
Arrays in C are a powerful tool for storing and processing collections of data. They are versatile, efficient, and can simplify your code by allowing you to store and manipulate large amounts of data with a single variable.
For Example:
int arr[5];
Above example declares an array
arr
of 5 integers.The first element of the array is stored at the first memory location, the second element is stored at the second memory location, and so on.
Arrays can be accessed using their index, which starts from 0. For example,
arr[0]
- refers to the first element of the arrayarr[1]
- refers to the second element, and so on.
Basic Example - arrays in C
Here is a basic example of using arrays in C:
#include <stdio.h>
int main()
{
int i;
int scores[5]; // declare an array of 5 integers
// initialize the elements of the array
scores[0] = 90;
scores[1] = 85;
scores[2] = 80;
scores[3] = 75;
scores[4] = 70;
// print the elements of the array
printf("The scores are:\n");
for (i = 0; i < 5; i++)
{
printf("%d\n", scores[i]);
}
return 0;
}
Output:
Explanation
- Here we declare an array of 5 integers named
scores
. - We then initialize the elements of the array with the values 90, 85, 80, 75, and 70.
- Finally, we use a
for
loop to print the elements of the array.
When to use arrays in C
Arrays in C are useful in situations where you need to store a large number of elements and perform operations on them, such as sorting, searching, or manipulating the elements in some way.
Here are a few examples of when arrays can be useful in C:
Store a large number of values of the same data type: If you need to store 100 test scores, you can use an array of 100
int
values to store the scores.Process collections of data: If you need to find the average of a set of numbers, you can store the numbers in an array and use a loop to process each number and calculate the average.
Store strings: In C, strings are typically stored as arrays of characters. For example, you can use an array of char values to store a string such as "Hello, World!".
Store a collection of structures: Arrays can be used to store collections of structures. For example, you can use an array of structures to store a collection of employee records.
When NOT to use arrays in C
There are a few cases when you might not want to use arrays in C:
When you only need a few elements: If you don't have many elements to store, using an array might be too complicated.
When you need to store different types of data: Arrays can only store elements of the same data type, so you can't use them if you need to store different types of data.
When you need a collection that can change size: Arrays have a fixed size, so they can't be used if you need a collection that can change size.
When you need a more complex data structure: Arrays are a basic data structure, and they might not be enough if you need something more complex.