Arrays in C
Arrays in C
Topperworld.in
Array in C
Declaration of Array
In C, we must 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 :
©Topperworld
C Programming
The C arrays are static in nature, i.e., they are allocated memory at the
compile time.
Example
// C Program to illustrate the array declaration
#include <stdio.h>
int main()
{
return 0;
}
Initialization of Array
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.
©Topperworld
C Programming
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.
©Topperworld
C Programming
Example
// C Program to demonstrate array initialization
#include <stdio.h>
int main()
{
©Topperworld
C Programming
Traversal of Array
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.
©Topperworld
C Programming
Types of Array
There are two types of arrays based on the number of dimensions it has. They
are as follows:
1. One Dimensional Arrays (1D Array)
2. Multidimensional Arrays
Syntax
array_name [size];
Example:
#include <stdio.h>
int main()
{
// 1d array declaration
int arr[5];
©Topperworld
C Programming
}
printf("Elements of Array: ");
// printing 1d array by traversing using for loop
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
2. Multidimensional Array
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. We can declare arrays with more dimensions than 3d arrays but they
are avoided as they get very complex and occupy a large amount of space.
Two-Dimensional Array
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.
Syntax
array_name[size1] [size2];
Here,
size1: Size of the first dimension.
size2: Size of the second
dimension.
©Topperworld
C Programming
Example of 2D Array
int main()
{
printf("2D Array:\n");
// printing 2d array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}
Output:
2D Array:
10 20 30
40 50 60
2.Example:
// C Program to print the elements of a Two-Dimensional array
#include <stdio.h>
int main(void)
{
// an array with 3 rows and 2 columns.
int x[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
©Topperworld
C Programming
return (0);
}
Output:
Element at x[0][0]: 0
Element at x[0][1]: 1
Element at x[1][0]: 2
Element at x[1][1]: 3
Element at x[2][0]: 4
Element at x[2][1]: 5
Advantage of Array
©Topperworld