Basic Concepts of an Arrays
Example with C
This Article Introduce to below this Topic’s
A.
Introduction of Array with example
B.
Types of Array with example
C.
Initialization of Arrays
D.
Example with C Program
A.
Introduction of 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 of group like object.
Each element
of the array can be referred to by the array name and a subscript or index
Syntax:-
Datatype arrayname [n];
B.
Types of Array with example
There are two types of array
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];
This defines an integer array called numbers. The array can
hold 10 integer values, which will be stored the elements numbers [0] to
numbers [9].
Float fnums [10];
This defines a float array called fnums. The array can hold
10 float values, which will be stored the elements fnums [0] to fnums [9].
Char name [10];
This defines a character array called name .the array can
hold 10 characters values, which will be stored the elements name [0] to name
[9].
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 at a 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 first case individual elements
have been moved into the array and hence it is essential that the name
terminator be specified explicitly.
In that second
case, the name terminator gets attached automatically because a name has been
assigned.
Int ages [] = {19, 23, 45.27, 28, 29};
In that case integer and float individual value have to be moved into the elements. In the proceeding declaration the dimension has not been specified for reason of flexibility, the compiler will automatically calculate the dimension based on the initialization.
In that case integer and float individual value have to be moved into the elements. In the proceeding declaration the dimension has not been specified for reason of flexibility, the compiler will automatically calculate the dimension based on the initialization.
Two-dimensional / Multi-dimensional
arrays
A
two-dimensional array is in essence an array of one –dimensional array.it is
also known as “two-d array”.
Syntax:-
Datatype arrayname[x]
[y];
Each
dimension of the two –d array has been placed in a separate set of brackets.
Two-d arrays are stored in a row –column matrix, in which the first index
indicates the row and second indicates the column.to access a specific element,
either the indices or subscripts have to be specified.
Initializing
Multi-dimensional/ Two-dimensional arrays
The rules for initialization of 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.
Ex: - int
squares [10] [2] =
{
{1, 1},
{1, 2},
{2, 3},
{3, 4},
{4, 5},
{5, 6},
{6, 7},
{7, 8},
{8, 9},
{9, 0}
};
Ex: - Int
sales [3] [4] = {
{143,
234},
{253,
567,546},
{786,
657, 890, 543}
};
Only the two elements of the first row, three in the second
row, and four in the third row are initialized.
As
usual, the array can also be initialized inside the function by accepting the
values.
Character arrays initialized in two-dimensional arrays
Char name [7] [3] = {
{‘Krishna’},
{‘Radikaa’},
{‘Govinda’}
};
No comments:
Post a Comment
Please Comment Your Valuable Feedback....