Arrays in C

Arrays in C

Arrays in C Language

An array is an order sequence of finite data items of the same type that share a common name. The common name is the array name.

Each individual data item in the in the list is known as element of the array.

The name of an array is actually the memory address from where the storing of the elements of an array starts. Which is at index 0.

The elements of an array are stored in the contiguous addresses in the memory starting from the address referred to by the array name.

An array may be one-dimensional, two-dimensional or multidimensional. One-dimensional array represents a list of data items and is also known as vector.

A two dimensional array represents a table of data items consisting of rows and columns and is known as a matrix.

Declaring one dimensional array

type array_name[size];

int marks[5];

char name[10];

Initializing an one dimensional array

int marks[5]={23,45,32,34,33};

Arrays in C
Arrays in C

char name[5]=”RAMA”;

Addressing array elements

An array can accommodate only as much elements as per its predefined size. Suppose marks[5] is an array with size equal to 5. The lower bound always starts with index value 0 and upper bound is size-1 i.e. 4. So valid array addresses are marks[0]-marks[4]. These storage locations are called array index or subscript, So to access array elements of array

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

marks[0]=1;  

marks[1]=2;

marks[2]=3;  

marks[3]=4;  

marks[4]=5;  

marks[0], marks[1], marks[2], marks[3], marks[4], are the addresses of the array elements.

Important points about array –

  • Array is an ordered sequence of same type of objects, called elements of the array.
  • Any array name has lvalue and this lvalue is non-modifiable.
  • We can’t assign one array to another array of the same type.
  • Since the array name refers to the starting address from where the storage of the elements of the array starts, you can assign array to a pointer of the same type as it is of the each element of the array.
  • The array elements are stored in contiguous locations in the memory. This means that if an array whose type is int, starts storing its elements from the address 1111 then the first element will be stored at the address 1111, second at 1113, third at 1115 and so on.
  • The array is not self describing that is array does not contain within it any information regarding what is the type of the element it stores or what is its size etc.
  • Array has no control over its bounds. That is if the size of an array is 5 then legally you can access elements from arr[0] to arr[4]. But illegal access like arr[-2] also run without any error message flagged by the compiler.
  • Memory is statically allocated for an array in c. That is all the information regarding allocation of memory is needed at the compile time. So any incomplete information will be insufficient and will lead to compile- time error. such as a[]; error because size of array is not mentioned.
  • Since array name is a pointer constant no arithmetic can be performed on it. So the following expressions are invalid for an array named arr.

arr++;

arr–;

arr+1;

arr-1;

  • An array of characters whose last element is ‘\0’ is known as a string.
  • An array cannot be passed to a function. However its starting address can be passed.
  • An array cannot be returned from a function. However, its starting address can be returned.
  • In traditional c the storage classes that can be applied on an array are static or external but not register. However in ANSI C it may also include automatic .
  • If an automatic array is not initialized, all its elements will contain garbage values. static or external arrays, if not initialized, have all their elements initialized to 0.
  • To initialize character arrays there are two methods –

         char name[]=”RAMA”;

         char name[]={‘R’,’A’,’M’,’A’};

  • The size of an array should be mentioned in its declaration and it must not be a variable. The array size must be a constant expression.
  • In C you can’t use a constant defined using the const keyword to specify the size of an array
main()
{
  const int size=10;
   int a[size];    /* invalid in c */
}

Two Dimensional Array

When an array have more than one rows and columns then it is called two dimensional array. A two dimensional array can be declared like this –

 int a[2][3];

here first array index is called row denoted by i and second column denoted by j. Hence you can initialize an array of 2×3 size

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

To access the array elements –

a[0][0]=1;

a[0][1]=2;

a[0][2]=3;

a[1][0]=4;

a[1][1]=5;

a[1][2]=6;

#include<stdio.h>
void main()
{
int i,j;
int Matrix[2][3]={{1,2,3},{3,4,5}};
printf("Processing array elements \n");
for(i=0;i<2;i++)
{
                for(j=0;j<3;j++)
                                printf("%d \t",Matrix[i][j]);
                printf("\n");
}
}

Three Dimensional / Multidimensional Array

A three dimensional array can be thought of a book where each page is like a table or a two dimensional array and page no represent the third dimension. A three dimensional array can be declared as:

int a[2][3][4];

To initialize the above mentioned array –

int a[2][3][4]={   {

                             {1,2,3,4},

                             {2,3,4,5},

                             {8,7,6,5}

                             },

                             {

                             {7,5,4,4},  

                             {3,4,5,3},

                             {9,7,6,5}

                             }

                             };

The above mentioned array can be initialized as below-

First two dimensional Array

a[0][0][0]=1;       a[0][0][1]=2;       a[0][0][2]=3;       a[0][0][3]=4;

a[0][1][0]=2;       a[0][1][1]=3;       a[0][1][2]=4;       a[0][1][3]=5;

a[0][2][0]=8;       a[0][2][1]=7;       a[0][2][2]=6;       a[0][2][3]=5;

Similarly second table can be display as under –

a[1][0][0]=7;       a[1][0][1]=5;       a[1][0][2]=4;       a[1][0][3]=4;

a[1][1][0]=3;       a[1][1][1]=4;       a[1][1][2]=5;       a[1][1][3]=3;

a[1][2][0]=9;       a[1][2][1]=7;       a[1][2][2]=6;       a[1][2][3]=5;

 

Similar Topics :

Control Structures in C        Input/Output functions in C                 Variables in C

You may also like :

What is New in HTML5      How To Enable JavaScript in Browsers           Multiple GST and Interstate Sales invoice

 

 

To Download Official TurboC Compiler from here

Please Subscribe to Stay Connected

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

DigitalSanjiv will use the information you provide on this form to be in touch with you and to provide updates and marketing.
Share via