Array in C programming language is a collection of fixed size data belongings to the same data type. An array is a data structure which can store a number of variables of same data type in sequence. These similar elements could be of type int, float, double, char etc.For Example
int age; /* Integer array of size 100 */ float salary; /* floating point array of size 10 */
Suppose, you want to store salary of 100 employees. You can store salaries of all employees by creating 100 variable of int data type individually like employeeSalary1, employeeSalary2, employeeSalary3 .... employeeSalary100. This is very tedious, time consuming and difficult to maintain method of storing 100 salaries.
C programming language provides support for array data structure, which solves the problem of storing N similar data. We can declare an integer array of length 100 to store salaries of 100 employees
In the above array declaration, we declared an array of name "employeeSalary" which can store maximum of 100 integer data in contiguous memory locations. We can access the individual elements of an array using their index.
- employeeSalary refers to the first element of array.
- employeeSalary refers to the second element of array.
- employeeSalary refers to the last element of array which is 100th element.
Suppose, we have an integer array of size 7 whose name is score. The base address or starting address of array score is 1000(base address is same as address of first element score) and the size of int be 2 bytes. Then, the address of second element(score) will be 1002, address of third element(score) will be 1004 and so on.
Arrays in Details
- Initialization of Array in C
- Accessing Array Elements in C
- Multi Dimensional Arrays in C
- Passing Array to a Function in C
- Returning Array to a Function in C
Important Facts about Arrays in C
- An array is a collection of variables of same data types.
- All elements of array are stored in the contiguous memory locations.
- The size of array must be a constant integral value.
- Individual elements in an array can be accessed by the name of the array and an integer enclosed in square bracket called subscript/index variable like employeeSalary.
- Array is a random access data structure. you can access any element of array in just one statement.
- The first element in an array is at index 0, whereas the last element is at index (size_of_array - 1).
- Less amount of code : Using array we can aggregate N variables of same data type in a single data structure. Otherwise we have to declare N individual variables.
- Easy access of elements : We can access any element of array using array name and index. We can access all elements serially by iterating from index 0 to size-1 using a loop.
- Easy to implement algorithms : Certain algorithms can be easily implemented using array like searching and sorting, finding maximum and minimum elements.
- Random Access : We can access any elements of array in O(1) time complexity.
Declaration of Array in C
Like any other variable in C, we must declare an array first before using it. Below is the syntax of declaring a one-dimensional array.
- array_name : It is a valid C identifier representing the name of the array.
- array_size : It is the maximum number of elements which can be stored in array. It must be an integer constant greater than zero.
- data_type : It represents the data type of the elements to be stored in array.
- An Array of 10 Integers
- An Array of 50 Characters.
- Two Dimensional Array of Integers.
Types of Array in C
There are two types of Arrays in C
- One Dimensional Array
- Multi Dimensional Array
- Single dimensional array is used to store data in sequential order.
- Single dimensional array contains only one subscript/index in their declaration, like
- In one dimensional array, we can access any element using one index reference, like marks.
- Array having more than one subscript variable/index is called multi dimensional array.
- Multi dimensional array is array of array.
- Multi dimensional array contains multiple subscript/index in their declaration, like
- In multi dimensional array, we can access any element using multiple index reference, like matrix.