Introduction
In the previous chapters, we explored various aspects of functions in C, including recursion and inline functions. In this chapter, we will focus on arrays. Arrays are fundamental data structures in C that allow you to store and manage collections of data efficiently. An array is a collection of elements of the same type, stored in contiguous memory locations.
What is an Array?
An array is a collection of elements, all of the same type, stored in contiguous memory locations. Each element in an array can be accessed using its index. Arrays are used to store multiple values in a single variable, making it easier to manage large amounts of data. Note that the array index starts with 0.
Syntax
The basic syntax for declaring an array in C is as follows:
data_type array_name[array_size];
data_type
: The type of elements stored in the array (e.g.,int
,float
,char
).array_name
: The name of the array.array_size
: The number of elements the array can hold.
Example: Declaring and Initializing an Array
Let’s look at a simple example to understand how to declare and initialize an array.
Example: Declaring and Initializing an Integer Array
#include <stdio.h>
int main() {
// Declaring an array of integers
int numbers[5] = {1, 2, 3, 4, 5};
// Accessing and printing array elements using a loop
for (int i = 0; i < 5; i++) {
printf("Element at index %d: %d\n", i, numbers[i]);
}
return 0; // Returning 0 to indicate successful execution
}
Output:
Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Element at index 3: 4
Element at index 4: 5
In this example, we declared an array of integers named numbers
with five elements. We then used a loop to access and print each element in the array.
Accessing Array Elements
Array elements are accessed using their index, which starts from 0. The first element of an array is at index 0, the second element is at index 1, and so on.
Example: Accessing Array Elements
#include <stdio.h>
int main() {
int numbers[3] = {10, 20, 30};
// Accessing and printing array elements
printf("First element: %d\n", numbers[0]);
printf("Second element: %d\n", numbers[1]);
printf("Third element: %d\n", numbers[2]);
return 0; // Returning 0 to indicate successful execution
}
Output:
First element: 10
Second element: 20
Third element: 30
Modifying Array Elements
Array elements can be modified by assigning new values to them using their index.
Example: Modifying Array Elements
#include <stdio.h>
int main() {
int numbers[3] = {10, 20, 30};
// Modifying array elements
numbers[0] = 100;
numbers[1] = 200;
numbers[2] = 300;
// Accessing and printing modified array elements
printf("First element: %d\n", numbers[0]);
printf("Second element: %d\n", numbers[1]);
printf("Third element: %d\n", numbers[2]);
return 0; // Returning 0 to indicate successful execution
}
Output:
First element: 100
Second element: 200
Third element: 300
Array Initialization
Arrays can be initialized at the time of declaration. If the array size is not specified, the compiler determines the size based on the number of elements in the initialization list.
Example: Array Initialization
#include <stdio.h>
int main() {
// Array initialization without specifying size
int numbers[] = {10, 20, 30, 40, 50};
// Determining the size of the array
int array_size = sizeof(numbers) / sizeof(numbers[0]);
// Accessing and printing array elements
for (int i = 0; i < array_size; i++) {
printf("Element at index %d: %d\n", i, numbers[i]);
}
return 0; // Returning 0 to indicate successful execution
}
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
Multi-Dimensional Arrays
C supports multi-dimensional arrays, which are arrays of arrays. The most common multi-dimensional array is the two-dimensional array, which can be used to represent a matrix or a table.
Example: Declaring and Initializing a Two-Dimensional Array
#include <stdio.h>
int main() {
// Declaring and initializing a 2D array
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
// Accessing and printing 2D array elements
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("Element at matrix[%d][%d]: %d\n", i, j, matrix[i][j]);
}
}
return 0; // Returning 0 to indicate successful execution
}
Output:
Element at matrix[0][0]: 1
Element at matrix[0][1]: 2
Element at matrix[0][2]: 3
Element at matrix[1][0]: 4
Element at matrix[1][1]: 5
Element at matrix[1][2]: 6
Simple C Programs to Demonstrate Arrays
Program 1: Finding the Largest Element in an Array
Example:
#include <stdio.h>
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int array_size = sizeof(numbers) / sizeof(numbers[0]);
int max = numbers[0];
for (int i = 1; i < array_size; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
printf("The largest element is: %d\n", max);
return 0; // Returning 0 to indicate successful execution
}
Output:
The largest element is: 50
Program 2: Summing Elements of an Array
Example:
#include <stdio.h>
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int array_size = sizeof(numbers) / sizeof(numbers[0]);
int sum = 0;
for (int i = 0; i < array_size; i++) {
sum += numbers[i];
}
printf("The sum of the elements is: %d\n", sum);
return 0; // Returning 0 to indicate successful execution
}
Output:
The sum of the elements is: 150
Program 3: Reversing an Array
Example:
#include <stdio.h>
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int array_size = sizeof(numbers) / sizeof(numbers[0]);
printf("Original array: ");
for (int i = 0; i < array_size; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
printf("Reversed array: ");
for (int i = array_size - 1; i >= 0; i--) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0; // Returning 0 to indicate successful execution
}
Output:
Original array: 10 20 30 40 50
Reversed array: 50 40 30 20 10
Conclusion
Arrays are a fundamental data structure in C that allow you to store and manage collections of data efficiently. By understanding how to declare, initialize, and manipulate arrays, you can write more efficient and organized code. Arrays are versatile and can be used in a wide range of applications, from simple data storage to complex data processing tasks. Understanding multi-dimensional arrays further expands your ability to handle more complex data structures and problems.