Arrays in C
Arrays in C are used to store multiple values of the same type in a single variable. They are fixed in size and can be accessed using indices. Arrays are one of the most fundamental data structures in C.
Declaring Arrays
Arrays are declared using the syntax: type array_name[size];
.
Here, type
is the data type of the elements, array_name
is the name of the array, and size
is the number of elements the array can hold.
#include <stdio.h>
int main() {
// Declare an array of 5 integers
int numbers[5] = {1, 2, 3, 4, 5};
// Access and print array elements
for (int i = 0; i < 5; i++) {
printf("%d\n", numbers[i]);
}
return 0;
}
Initializing Arrays
Arrays can be initialized at the time of declaration. If the size is omitted, the compiler determines the size based on the number of elements provided.
#include <stdio.h>
int main() {
// Initialize array without specifying size
int numbers[] = {10, 20, 30, 40, 50};
// Calculate size of the array
int size = sizeof(numbers) / sizeof(numbers[0]);
// Print array elements
for (int i = 0; i < size; i++) {
printf("%d\n", numbers[i]);
}
return 0;
}
Multidimensional Arrays
C supports multidimensional arrays, such as 2D arrays, which are often used to represent matrices or tables.
#include <stdio.h>
int main() {
// Declare and initialize a 2D array
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
// Access and print 2D array elements
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Dynamic Arrays
In C, dynamic arrays can be created using pointers and memory allocation functions like malloc
and free
.
#include <stdio.h>
#include <stdlib.h>
int main() {
int size;
// Get array size from user
printf("Enter the size of the array: ");
scanf("%d", &size);
// Allocate memory dynamically
int *numbers = (int *)malloc(size * sizeof(int));
// Check if memory allocation was successful
if (numbers == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
// Input array elements
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &numbers[i]);
}
// Print array elements
printf("Array elements: ");
for (int i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
// Free allocated memory
free(numbers);
return 0;
}
Common Array Operations
Arrays are often used for operations like searching, sorting, and traversing. Below are some examples:
Searching in an Array
#include <stdio.h>
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int size = sizeof(numbers) / sizeof(numbers[0]);
int target = 30;
int found = 0;
// Linear search
for (int i = 0; i < size; i++) {
if (numbers[i] == target) {
printf("Element found at index %d\n", i);
found = 1;
break;
}
}
if (!found) {
printf("Element not found\n");
}
return 0;
}
Sorting an Array
#include <stdio.h>
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int numbers[] = {50, 20, 40, 10, 30};
int size = sizeof(numbers) / sizeof(numbers[0]);
// Sort the array
bubbleSort(numbers, size);
// Print sorted array
printf("Sorted array: ");
for (int i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
Advantages of Arrays
- Efficient Access: Elements can be accessed in constant time using indices.
- Memory Efficiency: Arrays store elements in contiguous memory locations.
- Versatility: Arrays can be used to implement other data structures like stacks, queues, and matrices.
Limitations of Arrays
- Fixed Size: The size of an array is fixed at the time of declaration.
- Memory Wastage: If the array size is larger than required, memory is wasted.
- Insertion/Deletion: Inserting or deleting elements in the middle of an array is inefficient.