Module 4: ARRAYS
• Array in C is one of the most used data structures in C programming. It
is a simple and fast way of storing multiple values under a single name.
Here we will see about array declaration, definition, initialization, types
of arrays, array syntax.
• An array in C is a fixed-size collection of similar data items stored in
contiguous memory locations. It can be used to store the collection of
primitive data types such as int, char, float, etc., and also derived and
user-defined data types such as pointers, structures, etc.
C Array Declaration
• In C, we have to declare the array like any other variable before using
it. We can declare an array by specifying its name, the type of its
elements, and the size of its dimensions. When we declare an array in
C, the compiler allocates the memory block of the specified size to
the array name.
➔ Syntax of Array Declaration
// C Program to illustrate the array declaration
#include <stdio.h>
int main()
{
// declaring array of integers
int arr_int[5];
// declaring array of characters
char arr_char[5];
return 0;
}
C Array Initialization
• Initialization in C is the process to assign some initial value to the
variable. When the array is declared or allocated memory, the elements
of the array contain some garbage value. So, we need to initialize the
array to some meaningful value. There are multiple ways in which we can
initialize an array in C.
1. Array Initialization with Declaration
2. Array Initialization with Declaration without Size
3. Array Initialization after Declaration (Using Loops)
1. Array Initialization with Declaration
• In this method, we initialize the array along with its declaration. We
use an initializer list to initialize multiple elements of the array. An
initializer list is the list of values enclosed within braces { } separated
by a comma.
2. Array Initialization with Declaration
without Size
• If we initialize an array using an initializer list, we can skip declaring
the size of the array as the compiler can automatically deduce the size
of the array in these cases. The size of the array in these cases is equal
to the number of elements present in the initializer list as the
compiler can automatically deduce the size of the array.
3. Array Initialization after Declaration
(Using Loops)
• We initialize the array after the declaration by assigning the initial
value to each element individually. We can use for loop, while loop, or
do-while loop to assign the value to each element of the array.
Access Array Elements
• We can access any element of an array in C using the array subscript
operator [ ] and the index value i of the element.
array_name [index];
// C Program to illustrate element access using array subscript
#include <stdio.h>
int main()
{
// array declaration and initialization
int arr[5] = { 15, 25, 35, 45, 55 };
// accessing element at index 2 i.e 3rd element
printf("Element at arr[2]: %d\n", arr[2]);
// accessing element at index 4 i.e last element
printf("Element at arr[4]: %d\n", arr[4]);
// accessing element at index 0 i.e first element
printf("Element at arr[0]: %d", arr[0]);
return 0;
}
Update Array Element
• We can update the value of an element at the given index i in a
similar way to accessing an element by using the array subscript
operator [ ] and assignment operator =.
array_name[i] = new_value;
C Array Traversal
• Traversal is the process in which we visit every element of the data
structure. For C array traversal, we use loops to iterate through each
element of the array.
// C Program to demonstrate the use of array
#include <stdio.h>
int main()
{
// array declaration and initialization
int arr[5] = { 10, 20, 30, 40, 50 };
// modifying element at index 2
arr[2] = 100;
// traversing array using for loop
printf("Elements in Array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Types of Array in C
• There are two types of arrays based on the number of dimensions it
has. They are as follows:
• One Dimensional Arrays (1D Array)
• Multidimensional Arrays
• Syntax of 1D Array in C
Array of Characters (Strings)
• In C, we store the
words, i.e., a
sequence of
characters in the form
of an array of
characters terminated
by a NULL character.
These are called
strings in C language.
Multidimensional Array in C
• Multi-dimensional Arrays in C are those arrays that have more than
one dimension. Some of the popular multidimensional arrays are 2D
arrays and 3D arrays.
Two-Dimensional Array in C
A Two-Dimensional array or 2D array in C is an array that has exactly
two dimensions. They can be visualized in the form of rows and
columns organized in a two-dimensional plane.
See yourself
• Advantages
• Disadvantages
• Properties