Arrays in C
Arrays in C
Arrays:
An array is defined as the collection of similar type of data items stored at contiguous
memory locations.
An array is a group (or collection) of same data types.
An array is defined as finite ordered collection of homogenous data, stored in
contiguous memory locations.
An array is also known as a subscripted variable.
Arrays are the derived data type in C programming language which can store the
primitive type of data such as int, char, double, float, etc. It also has the capability to
store the collection of derived data types, such as pointers, structure, etc.
The array is the simplest data structure where each data element can be randomly
accessed by using its index number.
Each item in the array is called an element.
Every element of an array is identified by a number and is known as index / subscript
of an array element.
Array element index always start with zero (0).
One Dimensional
The array which have only one dimension i.e. elements in row or column fashion is
non as one dimensional array.
Declaration of array:
The array variable can be declared as
dataType arrayName[arraySize];
Example
int marks[5];
Index
0 1 2 3 5
Position
marks 44 55 66 44 33
marks[0] = 44;
marks[1] = 55;
marks[2] = 66;
marks[3] = 44;
marks[4] = 33;
To access array elements one can use any looping construct as shown in example below.
for(i=0;i<5;i++)
{
tot = tot + marks[i]);
}
}
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < 5; ++i)
{
printf("%d\n", arr[i]);
}
getch();
}
Example:
int a[3][3];
0 1 2
0 11 22 33
1 55 66 77
2 33 22 12
Example:
#include <stdio.h>
#include <conio.h>
main()
{
int a[3][3] = { {1,2,3}, {4,5,6}, {7,8,9}};
int i, j;