An array is a group or the collection of data having the same data-type stored in a contiguous memory location. It is a simple data structure format where the primitive type of data such as int, char, double, float, etc are stored and accessed randomly through their index number.
It can also store the collection of derived data types, such as pointers, structure, etc. Elements are arranged inside the array with index numbers starting from zero as the first elements.
Types of Array in C:
- One Dimensional Array
- Two Dimensional Array
- Multi-Dimensional Array
One Dimensional Array
An array with only one subscript is known as a one-dimensional array.
Example: int arr[10]
.
Syntax:
1 | data-type arrayName [ arraySize ]; |
Declaration of on Array:
For declaration of an array in C, the user needs to specify the type of element and number of elements in an array. Below is the declaration of single-dimensional Array.
1 | type arrayName [ arraySize ]; |
The data-type must be a valid C data type, a unique name must be specified to each array and the arraySize must be of an integer constant. Let see an example for integer array with 10 elements:
1 | int arr[10]; |
Initialization of an array:
Array can be initialize by using a single statement or one by one. The Initialization is done within the curly braces {}.
1 2 3 | int arr[5] = {45, 23, 99, 23, 90}; OR int arr[] = {45, 23, 99, 23, 90}; |
In the second one of the above example, the array size is not declared so that you can initialize as much value as you want.
Users can also directly assign the value to the particular element in an array by specifying the index number such as: below shows how to assign the value 23 to the 5th element of an array.
1 | arr[4] = 23; |
NOTE:
In the above example, number 4 means the 5th element as the array index starts from 0.
Accessing an Array:
As told above, that each array elements are associated with specific number that is the index numbers.
An array element can be accessed by using the specific index number. It can be achieved by placing the particular index number within the bracket []. Such as:
1 2 | arr[4]; //the 5th element is accessed arr[7]; //the 8th element is accessed |
We can also assign the value particular index number to other variable but their data type must be the same such as:
1 | int numb = arr[4]; |
In the above, the value of 5th element from an array arr[] is assigned to the numb variable.
Example: Demonstration for declaring, initializing, and accessing an array in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <stdio.h> int main() { /*declaration of an array with 10 elements*/ int arr[10]; int i, j; /*Array intitalization */ for (i = 0; i < 10; i++) { arr[i] = i + 1; } //accesing the array element and changing its value to 50 arr[5] = 50; //Displaying for (j = 0; j < 10; j++) { printf("Element[%d] = %d\n", j, arr[j]); } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 | Element[0] = 1 Element[1] = 2 Element[2] = 3 Element[3] = 4 Element[4] = 5 Element[5] = 50 Element[6] = 7 Element[7] = 8 Element[8] = 9 Element[9] = 10 |
Index Out of bound Checking: Accessing elements out of its bound.
Out of bound means, suppose you declare an array with 10 number of elements (index between 0 to 9) and you try to access the 11th element, which is not present in that array such as:
1 2 3 4 5 6 7 8 | ......... ........ { int arr[10]; printf("%d", arr[11]); //11th element not present ....... } |
In the above program, the 11th element is not present but we are trying to print that element. In such cases, the program may run fine but you may get an unexpected output (undefined behavior) from a program.
There is no index out of bounds checking in C.
Empty Members in an array
When we declare an array of size n, that means we can store the n number of elements of in that array. But what happens if we were to store less than n number of elements.
Consider an example below:
1 2 3 | //size=10 //stored = 6 elements int arr[10] = {19, 10, 8, 45, 2, 87}; |
As you can see, the size of an array is 10 but we initialized it with only 6 elements. However, a 10 contiguous memory location is already allocated to this array. What will happen to the remaining un-initialized arrays?
Now in such cases, random values are assigned by the compiler to the remaining places. And often these random values are 0.
Advantages of Array in C
- Less line of codes that is a single array with multiple elements, specifying code optimization.
- Elements of an array can be accessed randomly with the index number.
- The elements in an array can be easily traversed.
- Array’s data are easy to manipulate.
- Sorting becomes easy in an array with fewer lines of codes.
Disadvantages of Array in C
- The array is of fixed size once initialized. We cannot add new elements after initialization.
- The number of elements you need to store in an array must be known in advance.
- There may be a wastage of space if more memory is allocated than the required amount.
Learn in Detail:
1 | One-Dimensional Array |
2 | Two-Dimensional Array |
3 | Multi-Dimensional Array |
4 | Passing Array to Function |
5 | Pointers to Array |