In C, an array is a collection of elements of the same type, stored in contiguous memory locations. Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. Arrays in C are zero-indexed, meaning the first element is accessed with index 0
.
Declaration of Arrays
The basic syntax to declare an array is:
type array_name[array_size];
Where:
type
is the data type of the elements (e.g.,int
,char
,float
).array_name
is the name of the array.array_size
is the number of elements the array can hold.
Example: Declaring an Integer Array
int numbers[5];
This declares an array of 5 integers. However, the elements are not initialized by default, so they may contain garbage values.
Initializing Arrays
Arrays can be initialized at the time of declaration, which means assigning values to the elements.
int numbers[5] = {1, 2, 3, 4, 5};
Alternatively, if the array size is not specified, C will automatically deduce the size from the number of elements provided:
int numbers[] = {1, 2, 3, 4, 5}; // Size is automatically set to 5
You can also partially initialize an array:
int numbers[5] = {1, 2}; // Initializes first two elements, rest are 0
Accessing Array Elements
Array elements are accessed using the array index, starting from 0
:
int numbers[5] = {1, 2, 3, 4, 5}; printf("%d", numbers[0]); // Output: 1 printf("%d", numbers[4]); // Output: 5
Modifying Array Elements
You can modify individual elements of the array by accessing them using their index:
numbers[2] = 10; // Modify the third element (index 2)
Multi-Dimensional Arrays
In C, you can declare multi-dimensional arrays (e.g., matrices). A two-dimensional array can be thought of as an array of arrays.
Syntax
type array_name[rows][columns];
Example: Declaring a 2D array (matrix) with 2 rows and 3 columns:
int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} };
You can access elements in a 2D array like this:
printf("%d", matrix[0][1]); // Output: 2 (First row, second column)
You can also initialize a 2D array with specific values:
int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} };
Example of Iterating Over an Array
Using loops is the most common way to iterate through an array. Here’s an example of iterating through a 1D array:
#include <stdio.h> int main() { int numbers[5] = {1, 2, 3, 4, 5}; for (int i = 0; i < 5; i++) { printf("Element at index %d is %d\n", i, numbers[i]); } return 0; }
Output:
Element at index 0 is 1 Element at index 1 is 2 Element at index 2 is 3 Element at index 3 is 4 Element at index 4 is 5
For a 2D array, you can use nested loops:
#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("matrix[%d][%d] = %d\n", i, j, matrix[i][j]); } } return 0; }
Output:
matrix[0][0] = 1 matrix[0][1] = 2 matrix[0][2] = 3 matrix[1][0] = 4 matrix[1][1] = 5 matrix[1][2] = 6
Array of Strings
An array of strings is essentially an array of character arrays (strings). Each string is a null-terminated array of characters.
Example of an array of strings:
#include <stdio.h> int main() { char *fruits[] = {"Apple", "Banana", "Cherry"}; for (int i = 0; i < 3; i++) { printf("Fruit %d: %s\n", i + 1, fruits[i]); } return 0; }
Output:
Fruit 1: Apple Fruit 2: Banana Fruit 3: Cherry
Array as Function Arguments
You can pass an array to a function by passing the name of the array, which refers to the address of the first element.
Example:
#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
Array Limitations
- Fixed Size: Once an array is declared, its size is fixed. This means you cannot change the size of an array during runtime.
- Memory Management: Arrays do not handle dynamic memory allocation automatically. To handle dynamic arrays, you typically use pointers and functions like
malloc
,calloc
, orrealloc
.
Summary
- Arrays in C store multiple values of the same type in contiguous memory locations.
- They are zero-indexed and have a fixed size.
- Arrays can be one-dimensional or multi-dimensional (like matrices).
- Arrays can be initialized, and their elements can be accessed and modified using indices.
- The
for
loop is typically used to iterate over arrays.