Arrays

 Arrays :

An array is a collection of data(variables) of same data type. It is a fixed size sequential arrangement of data in memory. It consists of contiguous memory location. .

DECLARING AN ARRAY:

data_type arrayname[size];

Size of an array must be given when declared. It must be an integer greater than or equal to 0.

ACCESSING An ARRAY:

The members of an array can be accessed by it index. Index of an array is from 0 to size-1. Loops are used to access the members of an array.

e.g.

INITIALIZING AN ARRAY:

Initialization of array is done in following 3 ways.

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

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

a[0]=1;

a[1]=2;

a[2]=3;

a[3]=4;

POINTERS AND ARRAY:

The array name is a pointer to the first element of array i.e. it stores the address of first element of an array.

e.g

Now, we can say that when an array is declared, a pointer is made to store the base address of an array and subsequent memory location of its size are reserved for that array which are accessed with the help of this base address.Viewing as a pointer a[i], actually, can be accessed *(a+i).

e.g.

int a[3];

here, *a gives a[0]

*(a+1) gives a[1]  //a+1 points next location of array elements

*(a+2) gives a[2]  //a+2 points next location of array element a+1

Since, a[i] can be written as *(a+i),

which is equivalent to *(i+a),

which can also be written as i[a].

So, a[i] can also be written as i[a].

Indexing starts from 0 in C language because array name points to base address of a array.

e.g.

TYPES OF ARRAY:

1. 1-D array

2. Multidimensional array

MULTIDIMENSIONAL ARRAY:

A multidimenaional array can viewed as an array of arrays. Like a2D array can be seen as a array of an array ,a 3D array can be seen as an array of 2D array.

2-D array : It is generally, visualised, as a matrix with m and n columns.

Syntax for declaring a 2D array:

datatype matrixname[no. of rows][no.of columns];

e.g.

int a[3][6];

It declares a matrix of order 3×6. Actually,it is an array of size 3 whose each element is an array of size 6.Similar, interpretation can be done for multidimensional arrays.

INITIALISING A MULTIDIMENSIONAL ARRAY :

Its not necessary to write the size during initialisation of whole array at the time of declaration.

Initialising a 2D array:

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

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

Initialising a 3D array:

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

Similar initialisation can be done for other multi-D arrays

A 2-D array can be accessed using two loops.

e.g.

Here, the inner loop is for accessing each column and outer one for rows.

Similar to 1-D array, multidimensional array can also be accessed via pointers.

e.g

An element of matrix a[m][n] where m and n are rows and columns respectively, can be accessed as follows:

a[0][0] = *(*(a))

a[i][j] = *(*(a + i) + j)

a[i][j] = *(a[i] + j)

a[i][j] = (*(a + i))[j]

a[i][j] = *((*(a)) + (i * n+ j))

&a[i][j] = ((*(a)) + (i * n + j))

Leave a Reply

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