Arrays



  • An array is a group of element of same type sharing same name & stored in a consecutive memory location.
  • If you want to store multiple value of same type then an array can be created by syntax.
  • Array is a data structure, which provides the facility to store a collection of data of same type under single variable name.
  • Just like the ordinary variable, the array should also be declared properly.
  • The declaration of array includes the type of array that is the type of value we are going to store in it, the array name and maximum number of elements.
  • The type may be any valid type supported by C. Array names, like other variable names, can contain only letter, digit and underscore characters. Array names cannot begin with a digit character. i.e., The rule for giving the array name is same as the ordinary variable. The size should be an individual constant.
  • To refer to a particular location or element in the array, we specify the name of the array and the index of the particular element in the array. The index specifies the location of the element in the array. The array index starts from zero. The maximum index value will be equal to the size of the array minus one. The array index can be an integer variable for an integer constant.
  • Array name is the name of array which indicates collection of variable having same data type & has common characteristics.
  • It will reserve 10 byte of continuous memory.

Syntax:-
datatype array name[size];
Example:-
i) int a[100];
ii) int roll_no[5];

array
Need of array :

 void main()
  {
   Int roll_no;
   Roll_no=1;
   Roll_no=2;
   printf (ā€œ\n roll_no=%dā€, roll_no);
  }

This program will print the value of roll_no as 2. Because when a value 2 is assigned to roll_no, the earlier value of roll_no. ie. 1 is lost.

Thus, ordinary variable are capable of holding only one value at a time. However there are situations in which we would want to store more than one value at a time in a single variable.

Accessing Elements :

Elements of an array can be access by using the syntax.
Syntax: Array Name [index]
Note: Index number begin with zero

Initialization of Array

Array can be initialized at the time of its creation. While defining an array we can directly stored values into it.
Syntax:- Datatype array_name [size] = {list of value};

Arrays are classified into three types as follows :
  • One dimensional array
  • Two dimensional array
  • Three dimensional array

Dimension defines the number of rows and columns of the array i.e. dimension Specifies how many rows and columns are available to store the data.