Arrays in C programming | Introduction to Arrays

Arrays in C programming | Introduction to Arrays

Arrays in C Programming | Introduction and Basics

What is an Array?

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

Why Use Arrays?

  1. Efficient Memory Usage – Arrays store elements in a single block of memory.
  2. Easy Access – Elements can be accessed using an index.
  3. Improved Code Readability – Instead of using multiple variables, arrays simplify data management.
  4. Better Performance – Arrays provide faster data retrieval compared to individual variable access.

Declaring and Initializing Arrays

Declaration of an Array

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.

Initialization of an Array

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.


Accessing Array Elements

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

Types of Arrays in C

1. One-Dimensional Arrays

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

2. Multi-Dimensional Arrays (2D Arrays)

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

3. Three-Dimensional Arrays

A three-dimensional array can be used for storing multiple matrices.

Declaration:

int arr[2][3][4];  // 2 blocks, each having a 3x4 matrix

Array Operations

1. Traversing an Array

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

2. Inserting Elements into an Array

Elements in an array are modified using their index.

Example:

arr[2] = 100;  // Updates third element

3. Deleting an Element from an Array

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

4. Searching for an Element in an Array

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

5. Sorting an Array

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

Advantages of Using Arrays

  1. Faster Access – Elements are accessed in constant time O(1) using an index.
  2. Efficient Memory Management – Arrays help manage large data efficiently.
  3. Better Data Organization – Useful for sorting and searching algorithms.
  4. Easy Traversal – Iteration using loops simplifies operations.

Limitations of Arrays

  1. Fixed Size – Once declared, the array size cannot be changed dynamically.
  2. Insertion & Deletion Overhead – Adding or removing elements in the middle requires shifting.
  3. Wasted Memory – If the allocated size is larger than needed, memory is wasted.

Conclusion

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.

Arrays in C Programming | Introduction and Basics