Subscribe by Email


Monday, October 10, 2011

Some details about Multi dimensional arrays in C...

An array can be defined as the collection of variables of the same type that are referenced by a common name. Arrays are of two types namely one dimensional arrays and multi dimensional arrays. One dimensional array consists of finite homogenous elements whereas a multi dimensional array is composed of elements each of which is itself an array. Arrays refer to a named list of a finite number n of similar data elements. Each of the data elements can be referenced respectively by a set of consecutive numbers, usually 0, 1, 2, 3, 4,….., n. the simplest form of a multi dimensional array is the two dimensional array.

You can declare a two dimensional array as follows:
Type array name [ rows ] [ columns ];

Where type is the base data type of the array having name name, rows, the first index, refers to the number of rows in the array and columns, the second index refers to the number of columns in t5he array. For example:

Int sales [ 5 ] [ 10 ] ;

The general form of array initialization is shown below:

Type array name [ size N ] = { value list } ;

The value list is a comma separated list of array elements values. The element’s values in the value list must have the same data type as that of the base type of the array.
Int days [ 5 ] = { 1, 2, 3, 4, 5 } ;

Character arrays can also be initialized as shown below:
Char string [ 10 ] = { ‘c’ , ‘a’ , ‘t’ ‘\0’ } ;

Multi-dimensional arrays can also be initialized in the same way as simple dimensions one. For example:
Int abc [ 3 ] [ 2 ] = { 1, 1,
2, 2,
3, 3 } ;

This can be done it the other way also :
Int abc [ 3 ] [ 2 ] = { 1, 1, 2, 2, 3, 3 } ;

A multi dimensional array of strings can be initialized as shown below:

Type array name [ size N ] = { value list } ;

Here type declares the base type of the array, the array name specifies the name with which the array will be referenced and size defines how many elements the array will hold. The size must be an integer value or integer constant without any sign. The value list is a comma separated list of array’s elements values. The element values in the value list must have the same data type as that of the base type of the array.

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

Character arrays can also be initialized as shown below:
Char string [ 10 ] = { ‘c’ , ‘a’ , ‘t’ , ‘s’ , ‘\0’ } ;

Multi dimensional arrays are also initialized in the same as the single dimensional one. For example:
Int cube [ 3 ] [ 2 ] = { 1, 1,
2, 2,
3, 3 } ;

This can be done in the other way also:
Int abc [ 3 ] [ 2 ] = { 1, 1, 2, 2, 3, 3 } ;

A multi dimensional array of strings can be initialized as shown below:
Char abc [ 3 ] [ 2 ] = { “Sunday” , “Monday” } ;

C allows you to skip the size of the array in an array initialization statement. This is called unsized array initialization. C allows arrays of more than 2 dimensions. The exact limit of dimensions is determined by the compiler we are using.


No comments:

Facebook activity