An array is a sequence of data item of homogeneous value. It is used to store a collection of data but it is often more useful to think of ...
An array is a sequence of data item of homogeneous value. It is used to store a collection of data but it is often more useful to think of an array as a collection of variable of the same type. All array consist of contiguous memory location. The lowest address corresponds to the first element and the highest address to the last element.
for two dimensional array
storage_class data_type array_name[no of row][no of element in each row];
Example
int a[5][4];
Initialization of array
The initializer for an array is a comma separated list of constant expression enclosed in braces {}. The initializer is preceded by an equal sign. If an array is partially initialized, elements that are not initialized receive the value 0 of the appropriate type. We can not initialize more number than the declaration in the array.
Example
int number[3] = {3,5,7};
Multi-dimensional array
Multi-dimensional array are define in much the same manner as one-dimensional array, except that a separate pair of square brackets is required for each subscript. Two-dimensional array will require two pair and three required three pairs of square brackets. Therefore, syntax for multi-dimensional array is,
Syntax
storage_class data_type array_name[expression-1][expression-2]................[expression-n];
Example
float mda[20][10][2];
Passing array to a function
An entry array can be pass through a user define function as an argument. The way in which array is passed to the function differs from passing normal variable to a function.
function declaration, the name of array with appear of empty square bracket is passed as an argument.
Syntax
return_type function_name(data_type name[]);
Example
int sum(int a[]);
In function call the name of an array is passed without brackets as an actual argument.
Example
main()
int x[10];
.
.
.
.
sum(x);
In the function definition the array name is written with a pair of empty square brackets.
Example
void sum(int a[])
{
...............
...............
}
When an array is passed to function, values of array elements are not passed to the function but the address of first element is passed to function.
Declaration of array
An array declaration is very similar to variable declaration. First a type is given for the element of the array, then an identifier for the array and within square brackets the number of elements in the array. The number of element must be an integer.
Syntax
for one dimensional array
storage_class data_type array_name[size];
Example
int a[5];
storage_class data_type array_name[no of row][no of element in each row];
Example
int a[5][4];
Initialization of array
The initializer for an array is a comma separated list of constant expression enclosed in braces {}. The initializer is preceded by an equal sign. If an array is partially initialized, elements that are not initialized receive the value 0 of the appropriate type. We can not initialize more number than the declaration in the array.
Example
int number[3] = {3,5,7};
Multi-dimensional array
Multi-dimensional array are define in much the same manner as one-dimensional array, except that a separate pair of square brackets is required for each subscript. Two-dimensional array will require two pair and three required three pairs of square brackets. Therefore, syntax for multi-dimensional array is,
Syntax
storage_class data_type array_name[expression-1][expression-2]................[expression-n];
Example
float mda[20][10][2];
Passing array to a function
An entry array can be pass through a user define function as an argument. The way in which array is passed to the function differs from passing normal variable to a function.
function declaration, the name of array with appear of empty square bracket is passed as an argument.
Syntax
return_type function_name(data_type name[]);
Example
int sum(int a[]);
In function call the name of an array is passed without brackets as an actual argument.
Example
main()
int x[10];
.
.
.
.
sum(x);
In the function definition the array name is written with a pair of empty square brackets.
Example
void sum(int a[])
{
...............
...............
}
When an array is passed to function, values of array elements are not passed to the function but the address of first element is passed to function.
COMMENTS