This Tutorial explains an Array in C with Example(s).
What is an Array in C?
An array can be thought of as an arrangement of a specified number of elements of the same kind stored in contiguous memory locations and this arrangement is given a name.
Syntax of Array Declaration:
type name[size];
Here, Type is the Data type of an array (int or char).
Name – Name of the array.
Example:
int myArray[10];
Let’s try to declare ’12’ integers below,
int jan = 1, feb = 2, march = 3, april = 4, may = 5, june = 6, july = 7, august = 8, september = 9, october = 10, november = 11, december = 12; /* 12 integers declared */
Well! I’ve declared an initialized ’12’ integers. But what about if we need some 196 integers, let’s say, for population in 196 different countries. Is this declaration really easy? And what will happen when it comes to access such variables? When passing them in a function as arguments? Aren’t you in trouble? Fortunately, here is a way to fix this issue, declare an array, initialize it and use it!
Array Declaration and Initialization in C
count[10]; /* count an array of 10 something */
count is name of array immediately followed by a pair of subscripts ‘[]’ containing an expression which results in integer value specifying no. of elements in the array.
What’s type of 10 something? These something 10 can be integers, characters, floats etc. For example, if these are integers, then
int count[10]; /* count an array of 10 integers */
if these something are characters, then
char count[10]; /* count an array of 10 characters */ char *count[10]; /* count an array of 10 strings */
and so on.
Since, an array contains more elements of same type, it’s generally called as Vector as opposite to a single variable called Scalar. Elements in an array are stored in locations with offsets ranging from 0 through size of array minus 1. For example,
float marks[5] = {67, 89.5, 45.8, 60, 48.6}; /* marks contains 5 floats */
Let’s see how these 5 floats are organised in marks,
marks[0] = 67; marks[1] = 89.5; marks[2] = 45.8; marks[3] = 60; marks[4] = 48.6;
Notice that offsets begin with ZERO and last ‘valid one’ is 1 less than size of array. These offsets are also called as Indices.
Different Types of Array
There are two kinds of arrays in C: Single Dimensional Arrays and Multi Dimensional Arrays.
Single Dimensional Array
Single-dimensional arrays are the simplest type of array and are used to store a list of items. It is also called one-dimensional array. To declare a one-dimensional array, the size of the array must be specified. For example, an array with 10 elements would be declared as int myArray[10];
Multi Dimensional Arrays
Multi-dimensional arrays are used to store data in a table format. They are often used to represent matrices or grids. To declare a multi-dimensional array, the number of dimensions and the size of each dimension must be specified. For example, a 2-dimensional array with 10 rows and 20 columns would be declared as int myArray[10][20];
Advantages of Array in C
- They’re simple to use and understand.
- Arrays are very efficient for storing and accessing data.
- Arrays offer an easy way to keep track of related data items (i.e., by using indices).
- It allows you to directly access any element in the array without having to loop through all of the elements. This can improve the efficiency of your code, especially if you need to access a large number of elements.
Disadvantages of Array in C
- The size of the array must be fixed when it is created. This means that if you want to add or remove elements from the array, you must create a new array with the updated size.
- Arrays are not always efficient. If you want to find an element in an array, you must go through each element until you find the one you’re looking for.
- Inserting or deleting items from an array can be costly. This is because when you insert or delete an item from an array, all items in the array must be shifted in memory to make room for the new item or fill the gap left by the deleted item. This can take a lot of time, especially with large arrays.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Buy C Books
- Practice Computer Science MCQs
- Practice BCA MCQs
- Apply for Computer Science Internship
- Buy Computer Science Books