Arrays

triptiThis article has been authored by Tripti Gupta,who has primarily written Physics articles out here.She tends to make every complex physics concept look dead simple to perplexed minds and has exhaustively written articles, explaining various concepts and phenomenons, here at XAmplified

Table Of Content

Introduction


An array is an ordered arrangement of data elements of similar type.In case of an array, each variable name stores one entity (number or character) within itself. An array is actually another variable which collectively holds group of similar quantities that are, of similar type.

For instance, if sales made in a whole week are represented as :

Weekday MON TUE WED THUR FRI SAT SUN
Sales 400 30 62 90 175 80 267

And if weekdays are represented by indexes(integers) then we have sales as

Weekday 0 1 2 3 4 5 6
Sales 400 30 62 90 175 80 267

This represents a ‘sales’ array of integer numbers where each day’s sales can be traversed linearly by incrementing /decrementing the weekday or index.

Properties of an array


An array may consist of some datatype elements.For example, only integers, floats or characters.

Index of an array always starts with ‘0’

Array is stored in contagious memory locations and is also known as SUBSCRIPTED VARIABLE

Syntax of declaring an array


The syntax of declaring an array is as follows :

<data type> <array name>‘[‘ size of array ‘]’;

For example, int a[4]; represents an array having four integer values

Similarly, float b[10]; represents a float array with 10 floating point numbers

Calculating Array size


An array size depends upon its data type. Since an integer occupies 2 bytes of memory so an integer array of 10 numbers will occupy memory of 2 x 10 bytes or 20 bytes.

Similarly, size can be calculated for float and character type arrays.

Accessing elements of an Array


The individual array elements can be accessed using the index of that particular array element.
For example, in an array ‘a’ having 5 elements {5,6, 7,8,9}, the second element ‘6’ is referred by a[1] since index in array starts from ‘0’.

Array Initialization

Array can be initialized similar to variables by entering data at the time of array declaration. If not initialized, an array holds some garbage value by default.

For example, int a[5] = {6,9,2,3,4} ; initializes an integer array where

a [0] = 6, a [1] = 9, a [2] = 2, a [3] = 3, a [4] = 4

For example,in case of char str [ ] = { ‘A’ , ‘P’, ‘p’, ‘L’, ‘E’}; ,no size has been specified and therefore the array automatically calculates the size from the number of initialized values.

Here size of ‘str’ is 6 as it appends NULL ‘\0’ character after last alphabet to show end of characters.

In case of float b[10] = { 4,2,}; , first b[0] = 4 and b[1] = 2.Rest are initialized with some garbage value.

In case of int l[3]  = {4,6, 7, 8} ; , the C compiler does not give any error if index elements exceed the array size. The array values are taken as :

l[0] = 4, l[1] = 6, l[2] = 7

Array passing between functions

When a function passes an array to another function, it is referred as ‘call by reference’ by default and therefore any change in array gets reflected in the called function as well.