## Arrays And Single Dimension Arrays In C Language

### Arrays:

An array is a collection of variables of the same type that are referenced by a common name. Specific elements or variables in the array are accessed by means of an index into the array.

In C all arrays consist of contiguous memory locations. The lowest address corresponds to the first element in the array while the largest address corresponds to the last element in the array.

C supports both single and multi-dimensional arrays.

### Single Dimension Arrays:

Syntax
`type  var_name[ size ] ;`

where type is the type of each element in the array, var_name is any valid C identifier, and size is the number of elements in the array which has to be a constant value.

NOTE : In C all arrays use zero as the index to the first element in the array.

Syntax
`int array[ 5 ] ;`

which we might illustrate as follows for a 32-bit system where each int requires 4 bytes.

Syntax
```array[0]        12        loc^n 1000
array[1]        -345        loc^n 1004
array[2]        342        loc^n 1008
array[3]        -30000        loc^n 1012
array[4]        23455        loc^n 1016```

NOTE : The valid indices for array above are 0 .. 4, i.e. 0 .. number of elements - 1

For Example :- To load an array with values 0 .. 99

Syntax
```int x[100] ;
int i ;

for ( i = 0; i < 100; i++ )
x[i] = i ;```

Arrays should be viewed as just collections of variables so we can treat the individual elements in the same way as any other variables. For example we can obtain the address of each one as follows to read values into the array

CODE/PROGRAM/EXAMPLE
```for ( i = 0; i < 100; i++ ) {
printf( "Enter element %d", i + 1 ) ;
scanf( "%d\n", &x[i] ) ;
}```

NB : Note the use of the printf statement here. As arrays are normally viewed as starting with index 1 the user will feel happier using this so it is good policy to use it in “public”.

To determine to size of an array at run time the sizeof operator is used. This returns the size in bytes of its argument. The name of the array is given as the operand

Syntax
`size_of_array = sizeof ( array_name )  ;`

NOTE : C carries out no boundary checking on array access so the programmer has to ensure he/she is within the bounds of the array when accessing it. If the program tries to access an array element outside of the bounds of the array C will try and accommodate the operation. For example if a program tries to access element array[5] above which does not exist the system will give access to the location where element array[5] should be i.e. 5 x 4 bytes from the beginning of the array.

Syntax
```array[0]        12        loc^n 1000
array[1]        -345        loc^n 1004
array[2]        342        loc^n 1008
array[3]        -30000        loc^n 1012
array[4]        23455        loc^n 1016
array[5]        123        loc^n 1020```

This piece of memory does not belong to the array and is likely to be in use by some other variable in the program. If we are just reading a value from this location the situation isn’t so drastic our logic just goes haywire. However if we are writing to this memory location we will be changing values belonging to another section of the program which can be catastrophic.

### Initialising Arrays:

Arrays can be initialised at time of declaration in the following manner.

Syntax
`type array[ size ] = { value list };`
CODE/PROGRAM/EXAMPLE
```For Example :-
int i[5] = {1, 2, 3, 4, 5 } ;
i[0] = 1, i[1] = 2, etc.```

The size specification in the declaration may be omitted which causes the compiler to count the number of elements in the value list and allocate appropriate storage.

CODE/PROGRAM/EXAMPLE
`For Example :-     int i[ ]  =  { 1, 2, 3, 4, 5 } ;`
#array_in_c #arrays_in_c #matrix_multiplication_in_c #Single_Dimension_Arrays_in_c #Initialising_Arrays_in_c

### (New page will open, for Comment)

Not yet commented...