This article introduces the following topics:
- Introduction to arrays with examples
- Types of arrays with examples
- Initialization of arrays
- Example with C program
Introduction to Arrays
An array can be defined as an area in memory. This can be referred to by a common name. Arrays are used to set a group like objects.
Each element of the array can be referred to by the array name and a subscript or index.
Syntax:
- Datatype arrayname [n];
There are the following two types of arrays:
- One-dimensional / Single –dimensional array
- Two-dimensional / Multi-dimensional arrays
One-dimensional / Single –dimensional array
An array is declared as containing a certain datatype and internally they are stored as a contiguous set of these data types.
- Int numbers [10];
- Float fnums [10];
- Char name [10];
Initialization of One-dimensional/Single-dimensional Array
An array can be initialized, when declared by specifying the vales of some or all of its elements. This initialization is at the time of declaration is possible only outside a function unless it is declared static.
Initialization can also be done inside the function after the array has been declared by accepting the values.
- Char name [7] = {‘K’,’r’,’i’,’s’,’h’,’n’,’a’, \0’};
- Char name [7] = {‘Krishna’};
In that second case, the name terminator gets attached automatically because a name has been assigned.
- Int ages [] = {19, 23, 45.27, 28, 29};
Two-dimensional / Multi-dimensional arrays
A two-dimensional array is in essence an array of one–dimensional arrays. It is also known as a “two-d array”.
Syntax:
- Datatype arrayname[x] [y];
Initializing Multi-dimensional/ Two-dimensional arrays
The rules for initialization of a two-d array are the same as a one-dimensional array. Initialization at the time of declaration must be done outside the function or must be declared static within the function.
For example:
- int squares [10] [2] =
- {
- {1, 1},
- {1, 2},
- {2, 3},
- {3, 4},
- {4, 5},
- {5, 6},
- {6, 7},
- {7, 8},
- {8, 9},
- {9, 0}
- };
- Int sales [3] [4] = {
- {143, 234},
- {253, 567,546},
- {786, 657, 890, 543}
- };
As usual, the array can also be initialized inside the function by accepting the values.
Character arrays are initialized as two-dimensional arrays:
- Char name [7] [3] = {
- {‘Krishna’},
- {‘Radikaa’},
- {‘Govinda’}
- };

Krishna Rajput SinghPosted Nov 29, 2014, 2:08 AM
thnak u so mch for views and more donwloads