In C programming, an array is a collection of elements of the same data type stored in contiguous memory locations. Arrays allow you to store multiple values under a single variable name, making them highly useful for tasks that require handling multiple related data points efficiently.
This guide will walk you through arrays, their syntax, types, and practical applications in C programming.
Definition of Arrays
An array is a data structure that holds a fixed number of elements of the same type. Each element in an array is accessed using its index, with the first element at index 0
.
Types of Arrays in C
- One-Dimensional Arrays: Stores elements in a single row.
- Two-Dimensional Arrays: Stores elements in rows and columns (like a matrix).
- Multi-Dimensional Arrays: Extends the concept of two-dimensional arrays to more dimensions.
Syntax for Declaring Arrays
data_type array_name[size];
- data_type: Specifies the type of data (e.g.,
int
,float
,char
). - array_name: Name of the array.
- size: Number of elements the array can hold.
Example 1: Declaring and Initializing a One-Dimensional Array
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
printf("Element at index %d: %d\n", i, numbers[i]);
}
return 0;
}
Output:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50
Accessing Array Elements
Each element in an array is accessed using its index, which starts at 0
and ends at size - 1
.
Modifying Array Elements
You can update the value of an array element using its index:
numbers[2] = 100; // Updates the third element to 100
Example 2: Summing Elements of an Array
#include <stdio.h>
int main() {
int numbers[] = {5, 10, 15, 20, 25};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += numbers[i];
}
printf("Sum of array elements: %d\n", sum);
return 0;
}
Output:
Sum of array elements: 75
Two-Dimensional Arrays
A two-dimensional array is used to store data in a tabular format (rows and columns).
Syntax for Two-Dimensional Arrays
data_type array_name[rows][columns];
Example 3: Initializing a Two-Dimensional Array
#include <stdio.h>
int main() {
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Output:
1 2 3
4 5 6
Multi-Dimensional Arrays
Multi-dimensional arrays extend beyond two dimensions, such as a 3D array.
Example 4: Multi-Dimensional Array
#include <stdio.h>
int main() {
int cube[2][2][2] = {
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
printf("cube[%d][%d][%d] = %d\n", i, j, k, cube[i][j][k]);
}
}
}
return 0;
}
Output:
cube[0][0][0] = 1
cube[0][0][1] = 2
cube[0][1][0] = 3
cube[0][1][1] = 4
cube[1][0][0] = 5
cube[1][0][1] = 6
cube[1][1][0] = 7
cube[1][1][1] = 8
Arrays and Functions
You can pass arrays to functions for processing. However, arrays are passed by reference, meaning any changes made to the array in the function will reflect in the original array.
Example 5: Passing Arrays to a Function
#include <stdio.h>
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int numbers[] = {1, 2, 3, 4, 5};
printArray(numbers, 5);
return 0;
}
Output:
1 2 3 4 5
Limitations of Arrays
- Fixed Size: You must specify the array size at the time of declaration.
- Memory Usage: Arrays occupy contiguous memory, which can be restrictive for large data sets.
- Static Type: All elements must be of the same data type.
Dynamic Arrays
Dynamic memory allocation (using malloc
or calloc
) can be used to create arrays whose size is determined during runtime.
Example 6: Dynamic Array
#include <stdio.h>
#include <stdlib.h>
int main() {
int n = 5;
int *array = (int *)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
array[i] = i + 1;
}
for (int i = 0; i < n; i++) {
printf("%d ", array[i]);
}
free(array);
return 0;
}
Output:
1 2 3 4 5
Applications of Arrays
- Data Storage: Store multiple values like scores, marks, or items.
- Matrix Operations: Use two-dimensional arrays for mathematical computations.
- Sorting and Searching: Implement algorithms like Bubble Sort, Binary Search, etc.
Conclusion
Arrays are fundamental in C programming, allowing you to store and manipulate multiple values efficiently. Whether working with one-dimensional, two-dimensional, or multi-dimensional arrays, mastering them is essential for any C programmer. For further insights and practical examples, visit Master Coding Science.