What is Array in C explained with example?
In this tutorial you will learn about the arrays, definition, declaration of array and Initialisation of array with example.
Definition of Array
An array in C is a collection of elements of the same data type, stored in contiguous memory locations. Arrays in C are useful when you need to store and manage a large amount of data of the same data type. They enable you to store multiple values in a single variable and simply access and manipulate them using a single variable name and an index.
Arrays are commonly used in C for the following purposes:
- Data storage for large collections: Arrays allow you to store huge amounts of data in a single variable, making it easier to maintain and manipulate.
- Iteration and Traversal: Arrays enable you to quickly iterate through all of the array's elements using a loop, making it simple to perform operations on all of the elements.
- Data Structures: Arrays can be used to build data structures such as stacks, queues, and lists, which are commonly used in algorithms.
- Dynamic Memory Allocation: Arrays can be allocated dynamically, which means that the amount of memory assigned to the array can be altered during runtime. This is useful when the amount of data to be stored is unknown in advance.
- Performance: Because array members are stored in contiguous memory locations and can be accessed directly using the index, array operations are often faster than operations on other data structures like as linked lists.
Declaration of Array in C
To declare an array, you need to specify the data type, the name of the array, and the size of the array.
The syntax for declaring an array in C is as follows:
data_type array_name[array_size];
where:
data_type
is the data type of the elements in the array (e.g. int, float, char, etc.)array_name
is the name of the array variablearray_size
is the number of elements that the array can hold
For example, to declare an array of integers of size 10, you would write:
int my_array[10];
char name[20];
In this example, the data type of the elements in the array is "int", the name of the array variable is "my_array", and the size of the array is 10. All the 10 elements will be integers.
It's important to note that the size of the array is fixed when it is declared and cannot be changed afterwards. Also the array elements are indexed from 0 to size-1.
Array Initialization in C
In C, you can initialize an array at the time of declaration or after declaring it. Initializing an array means assigning values to the elements of the array.
There are two ways to initialize an array in C:
- At the time of Declaration: You can initialize an array at the time of declaration by providing a list of values inside curly braces {}. The values in the list are assigned to the elements of the array in the order they are listed. For example:
int my_array[] = {1, 2, 3, 4, 5};
In this example, the array "my_array" is being declared and initialized with the values 1, 2, 3, 4, and 5 in that order.
- After Declaring: You can also initialize an array after it has been declared by assigning values to its elements using a loop or individually.
int my_array[5];
my_array[0] = 1;
my_array[1] = 2;
my_array[2] = 3;
my_array[3] = 4;
my_array[4] = 5;
In this example, the array "my_array" is being declared and the values 1, 2, 3, 4, and 5 are assigned to the elements of the array individually.
Initialization using for loop
#include<stdio.h>
int main()
{
int my_array[5],i;
printf("Enter 5 Array Elements");
for(i=0;i<5;i++)
{
scanf("%d",&my_array[i]);
}
return 0;
}
It's important to note that when initializing an array, the number of values in the initializer list should match the size of the array. If there are too few values in the list, the remaining elements will be set to zero. If there are too many values, the extra values will be ignored.
Accessing Array elements in C
In C, arrays are indexed starting at 0. To access an element at a specific index, you use the array name followed by the index in square brackets. For example, if you have an array called "my_array" and you want to access the element at index 3, you would use the expression "my_array[3]". This will give you the value stored at that index in the array.
int my_array[5];
my_array[0] = 1;
my_array[1] = 2;
my_array[2] = 3;
my_array[3] = 4;
my_array[4] = 5;
printf("%d",my_array[3]); // It will print value at index 3