Arrays in C

Array is a collection of homogenous data stored under unique name. The values in an array is called as ‘elements of an array.’ These elements are accessed by numbers called as ‘subscripts or index numbers.’ Arrays may be of any variable type.

Array is also called as ‘subscripted variable.’

Types of an Array :
1. One / Single Dimensional Array
2. Two Dimensional Array

1. Single / One Dimensional Array in C:
The array which is used to represent and store data in a linear form is called as ‘single or one dimensional array’.

Syntax:

	  [size];

Example:

	int a[3] = {2, 3, 5};
	char ch[20] = "TechnoExam" ;
	float stax[3] = {5003.23, 1940.32, 123.20} ;

Total Size (in Bytes):

	total size = length of array * size of data type

In above example, a is an array of type integer which has storage size of 3 elements. The total size would be 3 * 2 = 6 bytes.

* MEMORY ALLOCATION OF ARRAY IN C:

Fig : Memory allocation for one dimensional array

Program :

/*  Program to demonstrate one dimensional array.
Creation Date : 10 Nov 2010 11:07:49 PM
Author :www.technoexam.com [Technowell, Sangli] */

#include <stdio.h>
#include <conio.h>

int main(void)
{
	int a[3], i;;
	clrscr();
	printf("\n\t Enter three numbers : ");
	for(i=0; i<3; i++)
	{
		scanf("%d", &a[i]);  // read array
	}
	printf("\n\n\t Numbers are : ");
	for(i=0; i<3; i++)
	{
		printf("\t %d", a[i]);  // print array
	}
	getch();
	return 0;
}

Output :


	Enter three numbers : 9 4 6
	
	Numbers are :	9	4	6_

Features of Array:
o Array size should be positive number only.
o String array always terminates with null character (‘\0’).
o Array elements are countered from 0 to n-1.
o Useful for multiple reading of elements (numbers).

Disadvantages of Arrays:
o There is no easy method to initialize large number of array elements.
o It is difficult to initialize selected elements.

Leave A Comment

Your email address will not be published. Required fields are marked *