You are on page 1of 5

Array:-

An array is a collection of variables or data items of same type The general form for declaring an array is as follows:
Data_type variable_name [ size of the array ] ;

that are referred through a common name.

For example an array consisting of five elements of type int, can be declared as follows: int arr [ 5 ] ; This declaration is like declaring the following five variables to all of type int. a [ 0 ] , a [ 1 ] , a [ 2 ], a [ 3 ], a [ 4 ] . These individual variables that together makeup the array are referred to a verity of different ways. We will call them through subscripted variables or indexed variables or elements of the array. The number in square bracket is called subscript or index. The indexes are starting from zero and ending with the integer that is one less than the size of the array. score [ x ] subscript (or) index. Array name (or) indexed variable name Arrays in memory:Generally an array variable in memory is described by two pieces of information. An address in memory (giving the location of the first byte for that variable). The type and size of that variable. Array variable is represented in memory is the same as an ordinary variable. The location of the various array index variables are always placed next to one another in memory. For example : int a [ 6 ] ;

After executing the above statement the computer reserves enough memory space to hold six variables of type int. The computer then remembers the address of the indexed variable, which is starting element of the array. When your program needs the address of any other indexed variable, the computer calculates the address for that indexed variable from the address of starting element of that array.

Address of a [ 0 ]

8080 8081 8082 8083 8084 8085

a[0] a[1] a[2] a[3] a[4] a[5]

Address of a[3]

8086 8087 8088 8089 8090 8091 8092 8093

There is no

Indexed variable a[6], but if there it would be here.

Initialization of Arrays: commas.

When initializing an array, the values for various

indexed variables are enclosed within the braces and separated by For example, the following statement is used to initializing the three indexed variables of an array my_array. int my_array [3] = { 10,20,30 }; The above declaration is equal to the following code: int my_array [3] ; my_array [0] = 10; my_array [1] = 20; my_array [2] = 30; If we initialize an array when it is declared, you can omit the size of the array as follows: int my_array [ ] = { 10,20,30 }; is equivalent to int my_array [3] = { 10,20,30 }; Arrays as arguments to the Functions: You can use both array indexed variables and entire array as arguments to the function. used an argument. It is also possible to use an entire array as argument to a function, but an argument is a new kind known as an array argument. When an array argument is plugged in for an array parameter, all that is given to the function is the address in the memory of the first indexed variable of the array argument. The array argument does not tell the size of the array to the function. When you have an array parameter to a function you normally must have another formal parameter of type int that gives the size of the An indexed variable can be used as an argument to the function is exactly the same way that any variable can be

array. The general syntax for a function prototype with an array parameter is as follows:

return_type function_name ( data_type array_name [ ] , ..) { ----------------------body of the function -------------------------}

For example: # include <iostream.h> void fillarray ( int a [ ] , int size ) ; const int size = 20 ; void main () { int score [ size ] ; fillarray ( a, size ); } void fillarray ( int a [ ] , int size ) { for ( int i=0; i < size; i++) cin >> a [i]; } In the above example, the formal parameter int a [ ] is an array parameter, is not quite call-by-value parameter but for more practice it behaves very much like call-by-reference parameter.

You might also like