An array is a collection of elements of the same data type stored in contiguous memory locations. It allows efficient storage and manipulation of multiple values using a single variable name.
For example, instead of declaring multiple variables:
int a1, a2, a3, a4, a5;
You can use an array:
int arr[5]; // Declares an array of 5 integers
The syntax for declaring an array is:
data_type array_name[array_size];
Example:
int numbers[5]; // Declares an array of 5 integers
Here, numbers
is an array of size 5 that can store five integer values.
Arrays can be initialized at the time of declaration:
cCopyEditint numbers[5] = {10, 20, 30, 40, 50};
If you omit the size, the compiler determines it automatically:
int numbers[] = {10, 20, 30, 40, 50};
All elements in an uninitialized array are set to zero by default in the global scope.
Example:
cCopyEditint arr[5] = {1, 2};
This initializes arr[0] = 1
, arr[1] = 2
, and the remaining elements as 0
.
Array elements are accessed using an index, which starts from 0
.
Example:
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
printf("First element: %d\n", numbers[0]); // Accessing first element
printf("Third element: %d\n", numbers[2]); // Accessing third element
return 0;
}
Output:
yamlCopyEditFirst element: 10
Third element: 30
A one-dimensional array stores elements in a linear sequence.
Example:
int arr[4] = {5, 10, 15, 20};
Accessing elements:
cCopyEditprintf("%d", arr[2]); // Output: 15
A two-dimensional array is an array of arrays, useful for matrix representation.
Declaration:
int matrix[3][3]; // A 3x3 matrix
Initialization:
int matrix[2][2] = { {1, 2}, {3, 4} };
Accessing elements:
printf("%d", matrix[1][0]); // Output: 3
A three-dimensional array can be used for storing multiple matrices.
Declaration:
int arr[2][3][4]; // 2 blocks, each having a 3x4 matrix
Using a loop, we can iterate through each element.
Example:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
for(int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
10 20 30 40 50
Elements in an array are modified using their index.
Example:
arr[2] = 100; // Updates third element
In C, you cannot directly delete an element, but you can shift elements to remove it logically.
Example:
for(int i = pos; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--; // Reduce array size
You can use a loop to search for an element.
Example:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int key = 30, found = 0;
for(int i = 0; i < 5; i++) {
if(arr[i] == key) {
printf("Element found at index %d\n", i);
found = 1;
break;
}
}
if(!found) {
printf("Element not found\n");
}
return 0;
}
Output:
Element found at index 2
Sorting helps arrange elements in increasing or decreasing order.
Example using Bubble Sort:
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for(int i = 0; i < n - 1; i++) {
for(int j = 0; j < n - i - 1; j++) {
if(arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[5] = {50, 20, 40, 10, 30};
int n = 5;
bubbleSort(arr, n);
for(int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
10 20 30 40 50
O(1)
using an index.Arrays in C provide an efficient way to store and manage multiple elements under a single variable name. By understanding different types of arrays and their operations, programmers can effectively utilize them for various applications like sorting, searching, and data manipulation. Despite their limitations, arrays remain a fundamental data structure in C programming.