Categories
C

Array in C

Array in C

Array in C:-

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

  • Array index starts from zero which is known as the lower boundary of an array.
  • Array upper boundary is (Array size-1).
  • To access each element of array called traversing of the 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 the n-1 dimensional array.
  • The double dimensional array is a collection of the single-dimensional array.

Array Declaration:-

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

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

Initialization of an array:-

In c we can initialize 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 the previous example:-

Int p[4]=5;
  • The 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 a constant pointer.
  • Turbo C does not allow zero-length array size but GCC allows 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 does not copy null character.
  • A double dimensional array is a collection of the single-dimensional array, where every single dimensional array must be used for a 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 the address of any element of a double array use
  • Address = base address+(row number * row size + col number) + size of element
  • The array can be represented as a 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

4 replies on “Array in C”

Hey would you mind letting me know which webhost you’re utilizing? I’ve loaded your blog in 3 different web browsers and I must say this blog loads a lot faster then most. Can you suggest a good web hosting provider at a reasonable price? Many thanks, I appreciate it!

Leave a Reply

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