Array in C with its types and examples
Array in C with its types and examples
Declaration of an Array
type variable_name[];
In the above example, type denotes the data type of the array and
variable_name denotes the name of the array.
Disadvantage of C Array
1). Time complexity: In insertion and deletion operations, Time
complexity increases.
2). Memory Wastage: Because arrays are fixed in size so if no
elements in an array, still it consumes all spaces.
3). Fixed In Size: Once you had declared the size of an array, it is not
possible to increase it.
Array example in C
#include <stdio.h>
int main()
{
int a=0;
int marks[4];
marks[0]=60;
marks[1]=10;
marks[2]=44;
marks[3]=8;
for(a=0;a<4;a++){
printf("%d \n",marks[a]);
}
return 0;
}
Output
60
10
44
8
Types of Array in C
There are 2 types of C arrays
1. One dimensional array
2. Multi dimensional array
o Two dimensional array
o Three dimensional array
o four dimensional array etc….
One Dimensional Array
This can be considered as array as a row, where elements are stored one
after another.
Syntax
data-type arr_name[array_size];
data-type: It denotes the type of the elements in the array.
arr_name: Name of the array. It must be a valid identifier.
array_size: Number of elements an array can hold.
#include <stdio.h>
int main()
{
int i,j,arr[2][2];
arr[0][0] = 10;
arr[0][1] = 20;
arr[1][0] = 30;
arr[1][1] = 40;
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
{
printf("value of arr[%d] [%d] : %d\n",i,j,arr[i][j]);
}
}
return 0;
}
Output
value of arr[0] [0] : 10
value of arr[0] [1] : 20
value of arr[1] [0] : 30
value of arr[1] [1] : 40