Array in C Programming
An array in C is a collection of elements of the same type, stored in a contiguous block of memory. Each element in the array can be accessed using an index, making it an efficient way to store and handle a series of values in a single variable. Following are the major characteristics of an Array
- Fixed Size: Size of an array (in many languages) is set at the time of declaration and cannot be changed. This case is not always true.
- Same Data Type: All elements must be of the same data type (e.g., int, float, char).
- Indexed Access: Arrays are accessed via indexes, starting from 0, which provide fast access.
Following diagram shows the integer, float, and character type of array with different size.
Various types of array and their Declaration
In programming, declaring an array involves specifying its data type, name, and size. This process allocates a fixed amount of memory for storing elements that share the same data type..
Arrays are declared using square brackets with the size specified inside the brackets. Here is the syntax:
- 1D Array: int arr[n];
Declares a 1-dimensional array with n elements. - 2D Array: int arr[rows][columns];
Declares a 2-dimensional array with rows and columns. - 3D Array: int arr[blocks][rows][columns];
Declares a 3-dimensional array with blocks, each containing a grid of rows and columns.\ - 4D Array: int arr[sections][blocks][rows][columns];
Declares a 4-dimensional array with sections, where each section contains blocks, and each block contains a grid of rows and columns.. For example: int arr[3][4][2][5]; - 5D Array: int arr[levels][sections][blocks][rows][columns];
And so on for 6D and 7D to ND.
How Do You Initialize an Array?
Initializing an array means assigning values to its elements. There are several ways to initialize arrays in C:
Method 1: Initialize at declaration with values.
int a[6] = {2, 3, 5, 7, 11, 13};
Method 2: Declare without size and initialize. It works only for compile run, not for runtime.
int arr[] = {2, 3, 5, 7, 11};
Method 3: Initialize with user input.
int n; scanf("%d", &n); // Define array size based on user input int arr[n]; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); // Initialize each element }
Method 4: Manually set each element.
int arr[5]; arr[0] = 1; arr[1] = 2; arr[2] = 3; arr[3] = 4; arr[4] = 5;
How Can You Access Elements of an Array?
To access elements, use the array name followed by the index in square brackets. For example:
int arr[5] = {10, 20, 30, 40, 50}; printf("%d", arr[2]); // Access the third element, which is 30
In this example, arr[2] gives the value 30 because array indices start from 0. So arr[0] is the first element, arr[1] is the second, and so on.
Common Array Operations
1. Accessing Elements: Use the index to access an element.
printf("%d", array[2]); // Access the third element, output: 30
2. Updating Elements: Modify an element at a specific index.
array[1] = 25; // Changes the second element to 25
3. Inserting Elements: Since arrays in C have a fixed size, you can’t add more elements, but you can modify existing elements within the array’s size limit.
4. Deleting Elements: In a fixed-size array in C, you can’t reduce the size, but you can set elements to a specific value to represent “deletion”.
array[2] = 0; // Sets the third element to 0, effectively "deleting" its value
Important:
it’s possible to simulate insertion and deletion in arrays through more complex coding. Insertion and deletion by shifting elements has a time complexity of O(n), where n is the number of elements in the array, making it inefficient for large arrays. |
5. Searching in an Array: To search for an element in an array, you can use linear search (checking each element one by one) or binary search (if the array is sorted).
Example: Linear Search in C
#include <stdio.h> int main() { int arr[] = {10, 20, 30, 40, 50}; int n = 5, target = 30, found; for (int i = 0; i < n; i++) { if (arr[i] == target) { found = i; // Store index if found break; } } printf("Element found at index: %d\n", found); return 0; }
6. Sorting in an Array
To sort an array, you can use selection sort, bubble sort, or more efficient methods like quick sort.
Example: Bubble Sort in C
#include <stdio.h>
int main() {
int arr[] = {50, 20, 10, 40, 30};
int n = 5, temp;
for (int i = 0; i < n – 1; i++) {
for (int j = 0; j < n – i – 1; j++) {
if (arr[j] > arr[j + 1]) { // Swap if the current element is greater
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < n; i++) {
printf(“%d “, arr[i]); // Output: 10 20 30 40 50
}
return 0;
}
;}
Output
10 20 30 40 50
Advantages of Array in C Programming
- Fast Access: Accessing elements by index is very quick.
- Memory Efficiency: Arrays are stored in contiguous memory locations.
- Simple Data Manipulation: It’s easy to manipulate data with loop structures.
Disadvantages of Array in C
- Fixed Size: Once declared, the size of the array cannot be changed.
- Same Data Type Requirement: All elements must be of the same type.
- Inefficient Insertions/Deletions: Adding or removing elements from the middle requires shifting elements, which is time-consuming.
Conclusion
Arrays in C provide an efficient way to store and access a sequence of values. They have a fixed size and require all elements to be of the same type, making them memory efficient. Mastering arrays is a fundamental step in learning more advanced data structures in C.