Arrays in C: A Comprehensive Guide

Learn about the fundamental concept of arrays in C programming. Understand how arrays store collections of elements of the same data type, and explore the different types of arrays, including one-dimensional, multi-dimensional, and variable-length arrays. Discover how to declare, access, manipulate, and pass arrays as arguments to functions, making them a versatile tool for your C programs.



Arrays in C

Arrays in C are a kind of data structure that can store a fixed-size sequential collection of elements of the same data type. Arrays are used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

What is an Array in C?

An array in C is a collection of data items of similar data type. One or more values of the same data type, which may be primary data types (int, float, char), or user-defined types such as struct or pointers can be stored in an array. In C, the type of elements in the array should match with the data type of the array itself.

The size of the array, also called the length of the array, must be specified in the declaration itself. Once declared, the size of a C array cannot be changed. When an array is declared, the compiler allocates a continuous block of memory required to store the declared number of elements.

Why Do We Use Arrays in C?

Arrays are used to store and manipulate similar types of data. Suppose we want to store the marks of 10 students and find the average. We declare 10 different variables to store 10 different values as follows:

int a = 50, b = 55, c = 67, . . . ;
float avg = (float)(a + b + c +. . . ) / 10;

These variables will be scattered in the memory with no relation between them. Importantly, if we want to extend the problem of finding the average of 100 (or more) students, then it becomes impractical to declare so many individual variables.

Arrays offer a compact and memory-efficient solution. Since the elements in an array are stored in adjacent locations, we can easily access any element in relation to the current element. As each element has an index, it can be directly manipulated.

Example: Use of an Array in C

To go back to the problem of storing the marks of 10 students and find the average, the solution with the use of an array would be:

#include <stdio.h>

int main(){
int marks[10] = {50, 55, 67, 73, 45, 21, 39, 70, 49, 51};
int i, sum = 0;
float avg;

for (i = 0; i <= 9; i++){
sum += marks[i];
}

avg = (float)sum / 10;
printf("Average: %f", avg);
return 0;    
}
Output:
Average: 52.000000

Declaration of an Array in C

To declare an array in C, you need to specify the type of the elements and the number of elements to be stored in it.

type arrayName[size];

The size must be an integer constant greater than zero and its type can be any valid C data type. There are different ways in which an array is declared in C.

Example: Declaring an Array in C

In the following example, we are declaring an array of 5 integers and printing the indexes and values of all array elements:

#include <stdio.h>

int main(){
int arr[5];
int i;

for (i = 0; i <= 4; i++){
printf("a[%d]: %d\n", i, arr[i]);
}
return 0;
}
Output:
a[0]: -133071639
a[1]: 32767
a[2]: 100
a[3]: 0
a[4]: 4096

Initialization of an Array in C

At the time of declaring an array, you can initialize it by providing a set of comma-separated values enclosed within the curly braces {}.

data_type array_name [size] = {value1, value2, value3, ...};

Example to Initialize an Array

The following example demonstrates the initialization of an integer array:

#include <stdio.h>

int main() 
{
int numbers[5] = {10, 20, 30, 40, 50};

int i;  // loop counter

// Printing array elements
printf("The array elements are : ");
for (i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}

return 0;
}
Output:
The array elements are : 10 20 30 40 50

Example of Initializing all Array Elements to 0

To initialize all elements to 0, put it inside curly brackets:

#include <stdio.h>

int main(){
int arr[5] = {0};
int i;

for(i = 0; i <= 4; i++){
printf("a[%d]: %d\n", i, arr[i]);
}
return 0;
}
Output:
a[0]: 0
a[1]: 0
a[2]: 0
a[3]: 0
a[4]: 0

Example of Partial Initialization of an Array

If the list of values is less than the size of the array, the rest of the elements are initialized with "0".

#include <stdio.h>

int main(){
int arr[5] = {1,2};
int i;

for(i = 0; i <= 4; i++){
printf("a[%d]: %d\n", i, arr[i]);
}
return 0;
}
Output:
a[0]: 1
a[1]: 2
a[2]: 0
a[3]: 0
a[4]: 0

Example of Partial and Specific Elements Initialization

If an array is partially initialized, you can specify the element in the square brackets.

#include <stdio.h>

int main(){
int a[5] = {1,2, [4] = 4};
int i;

for(i = 0; i <= 4; i++){
printf("a[%d]: %d\n", i, a[i]);
}
return 0;
}
Output:
a[0]: 1
a[1]: 2
a[2]: 0
a[3]: 0
a[4]: 4

Getting Size of an Array in C

The compiler allocates a continuous block of memory. The size of the allocated memory depends on the data type of the array.

Example 1: Size of Integer Array

#include <stdio.h>

int main(){
int a[5];
printf("Size of array a: %lu", sizeof(a));
return 0;
}
Output:
Size of array a: 20

Example 2: Size of Char Array

#include <stdio.h>

int main(){
char a[5];
printf("Size of array a: %lu", sizeof(a));
return 0;
}
Output:
Size of array a: 5

Example 3: Size of Float Array

#include <stdio.h>

int main(){
float a[5];
printf("Size of array a: %lu", sizeof(a));
return 0;
}
Output:
Size of array a: 20