Array in C | Basic IT Topic

Array in C

Array in C

Array in C:-

Array in C  is a variable which can hold more than one similar types of element .It is sequential in nature.

  • Array index starts from zero which is known as lower boundary of an array .
  • Array upper boundary is (Array size-1) .
  • To access each element of array called traversing of array .
  • The main objective of an array to create a list.

Array in C

 

There are  two types of array :-

  1. Single dimensional Array
  2. Double dimensional Array
  • A dimensional array is a collection of n-1 dimensional array.
  • Double dimensional array is a collection of single dimensional array.

Array Declaration:-

To declare an array it requires the type of element and the number of element required by an array.

Type arrayname[arraysize];
Ex:- int p[5];

Initialization of an array:-

In c we can initialise of an array by individually and by a statement.

Ex:-

Int p[5]={1,2,3,4,5};

To initialize a single element of an array

As per previous example:-

Int p[4]=5;

 

  • Array name is a constant pointer which gives the address of the 0th element of an array
  •   Limitation of an array is

a) Unused memory of an array can’t be used

b) Array size can’t be changed in run time

  • One array can’t be assigned to another array, because both are constant pointer.
  • Turbo C does not allow zero length array size but GCC allow zero length and variable length array size.
  • The size of the zero length array size is 0
  • One array can not directly copy to another array by just equating two.
  • Array name can not be incremented and decremented, because it is a constant pointer.
  • Malloc memory allocation is single dimensional array equivalent and calloc memory allocation is double dimensional array equivalent
  • The major difference between strcpy and strncpy is strcpy copy the whole string includes null character but strncpy dose not copy null character.
  • Double dimensional array is a collection of single dimensional array, where each single dimensional array must be used for different purpose
  • Visiting double dimensional array in row major is always faster than column major.
  • Array name + 1 = Address of next element if it is single dimensional array
  • Array name + 1 = Address of next array if it is double dimensional array
  • To get address of any element of a double array use
  • Address = base address+(row number * row size + col number) + size of element
  • Array can be represented as subscript and pointer format.

 

Program to access each element of an array:-

main()

{

int p[5]={1,2,3,4,5};

int i;

for(int i=0;i<5;i++)

{

printf("%d",p[i]);

}

}

O/P=1 2 3 4 5
basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community