Arrays
A
Arrays are indexed
i d d variables
i bl that
th t contain
t i many data
d t items
it
(variables) of the same type. They are used to declare many
variables with a single declaration
declaration.. This saves coding time
and space.
Further, once declared, the compiler
p reserves the required
q
amount of memory addresses for the variables in contiguous
regions of the memory.
memory. This makes the access to all the
variables easy if any single address is known
known.
In C, one can have an array of anything: characters,
integers floats,
integers, floats doubles,
doubles arrays,
arrays pointers
pointers, structures,
structures and so
on.
In essence,, an arrayy is not a new variable type,
yp , rather this is
a method of declaring a number of variables of the same
1
type in groups.
Arrays
The syntax for array declaration is:
array_type array_name [array_size]
Examples are:
int array2 [12]; // An array of 12 integers (12 integer variables)
char array1 [10]; // An array of 10 characters
An integer constant must be used inside the square
bracket during array declaration. A variable name/expression
cannot be used inside the square brackets to declare the
arraysize.
arraysize
However constant (variable) name can be used inside the
square brackets but only through Macro definition like shown
below:
#define ARRAY_SIZE 10 // Macro declaration
int array_name[ARRAY_SIZE]; // Array declaration
All elements (variables) must be of the same type, i.e.,
2
arrays using mixed data (variable) types are not possible
Arrays
An array has four basic properties:
Thee individual
d dua data items
te s ((variables)
a ab es) in tthe
eaarray
ay a
are
e ca
called
ed
elements
All elements (variables) must be of the same data type
All elements are stored contiguously in the computer’s
memory, and the subscript (or index) of the first element is
always zero
[ that means the last element in the array has index of
(array_size--1) ]
(array_size
The array_name is a pointer that holds the address of the
first element in the array
[ All the subsequent elements can be accessed by using the
pointer arithmetic ]
3
Arrays
Example:
int st_id[5];
[ ]; // creates 5 elements ((variables)) st_id[0],
[ ],
// st_id[1], st_id[2], st_id[3] and st_id[4]
st_id
// creates a pointer with the arrayname
100
// the pointer points to the first element (variable)
[040]
st_id[0] st_id[1] st_id[2] st_id[3] st_id[4]
0 0 0 0 0
[100] [102] [104] [106] [108]
st_id
t id = &
&st_id[0];
t id[0] //N
//Nott necessary (automatically
( t ti ll done)
d )
4
Arrays
So the value of the first element can be accessed as:
st_id[0]
st_id[0] or as **st_id
st_id
The subsequent elements can be accessed as:
array_name[index]
array_name [index] or as *(array_name
*(array_name + index)
where, index = [[element_number
element_number – 1].
In the previous example, the last element (5 (5th element
element)) can be
accessed as:
st_id[4]
st_id [4] or as *(st_id+4)
In case of ppointer notation,, the index is automatically y multiplied
p
by the sizeof
sizeof((pointer_type)
pointer_type) to access a specific element in the
memory. For example, in case of integer type pointer, the index
is multiplied by 2 (byte)
(byte), character type pointer
pointer, the index is
multiplied by 1 (byte) and float type pointer, the index is 5
multiplied by 4 (byte) to access the element.
Arrays
There are three methods for initializing arrays:
Byy de
default
au t when
e tthey
ey a
are
e ccreated
eated – o
only
y app
applies
es to g
global
oba
and static automatic arrays
Only constant data can be used to initialize an array thus
created
t d
Explicitly when they are created by supplying constant
initializingg data
During program execution by assigning or copying data
into the array
The arrays will always be initialized (default) to binary
zero if no other initialization data is supplied
Th following
The f ll i program shows h suchh arrays:
6
Arrays
#include <stdio.h>
#define ARRAY_ONE_SIZE 5
#define ARRAY_TWO_SIZE 5
int array_one [ARRAY_ONE_SIZE]; // Global array
main ()
{
static int array_two [ARRAY_TWO_SIZE];
printf (“array_one [0]: %d\n”, array_one [0]);
printf (“array_two [0]: %d\n”, array_two [0]);
return (0);
}
Unlike programs in other languages, the first subscript for
allll arrays iin C iis zero
7
Arrays
The arrays can be initialized and ANSI C standard lets
supply initialization values for any array, global or
otherwise,
th i d
defined
fi d anywhere
h iin a program
The following code segment illustrates this –
int numbers [3] = {1,
{1 2,
2 3};
static float cost [5] = {5.45, 6.78, 3.88, 9.12, 0.0};
static int more_numbers [3] = {1, 2, 3, 4, 5, 6, 7};
char vowels [] = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’};
The first statement declares the numbers array to be 3
integers and provides the initial values of the elements
In the second example, initialization happens when the
entire program loads
8
Arrays
In the third example, there are 7 values for a 3-element
array
This will result in an error message indicating too many
initializers; in contrast, if there are more spaces than the
initializers, the values go into the beginning of the array
andd th
the extra
t elements
l t become
b zeroes
If the count is empty, as in the fourth example, the number
of values determines the size of the array
That is, the program dimensions the array automatically
with unsized arrays
Whenever C encounters an array initialization statement
and the array size is not specified, the compiler
automatically creates an array large enough to hold all of
the specified
p data
9
Arrays
It is possible to have arrays within arrays – that is,
multidimensional arrays such as shown below:
int multi_array [2] [3];
M ltidi
Multidimensional
i l arrays are d
declared
l db by iincreasing
i ththe
pair of square brackets following the array name.
If there is only one set of brackets, the array is one
dimensional, two sets of brackets indicate a two-
dimensional array, and so on.
Multidimensional arrays are stored in a linear fashion in the
computer’s memory – elements are grouped from
rightmost index inward
10
Arrays
Multidimensional arrays can be initialized in the same way
as one-dimensional array, for example:
float a [2] [3] = {3.1, 1.2, 0.5, 9.3, 2.4, 5.3};
Note that rightmost dimension always increases the fastest
a
100
[078] a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
31
3.1 12
1.2 05
0.5 93
9.3 24
2.4 53
5.3
[100] [104] [108] [112] [116] [120]
The usual terminology
gy for the two indices is that the first
gives the row number in the grid and that the second gives
the column number in the grid
If we declare an array as: int a[4][5];
then we can think of the array elements arranged as: 11
Arrays
col 1 col 2 col 3 col 4 col 5
row 1 a[0][0] a[0][1] a[0][2] a[0][3] a[0][4]
row 2 a[1][0] a[1][1] a[1][2] a[1][3] a[1][4]
row 3 a[2][0]
[2][0] a[2][1]
[2][1] a[2][2]
[2][2] a[2][3]
[2][3] a[2][4]
[2][4]
row 4 a[3][0] A[3][1] A[3][2] a[3][3] a[3][4]
a Actual Memory Mapping for declaration: int a[4][5];
100
[065] a[0][0] a[0][1] a[0][2] a[0][3] a[0][4] a[1][0] a[1][1] a[1][2] a[1][3] a[1][4]
0 0 0 0 0 0 0 0 0 0
[100] [102] [104] [106] [108] [110] [112] [114] [116] [118]
a[2][0] a[2][1] a[2][2] a[2][3] a[2][4] a[3][0] A[3][1] A[3][2] a[3][3] a[3][4]
0 0 0 0 0 0 0 0 0 0
12
[120] [122] [124] [126] [128] [130] [132] [134] [136] [138]
Arrays
If we declare an array as: int a[5][4];
col 1 col 2 col 3 col 4
row 1 a[0][0] a[0][1] a[0][2] a[0][3]
row 2 a[1][0] a[1][1] a[1][2] a[1][3]
row 3 a[2][0] a[2][1] a[2][2] a[2][3]
row 4 a[3][0] A[3][1] A[3][2] a[3][3]
row 5 A[4][0] A[4][1] A[4][2] A[4][3]
a
Actual Memory Mapping for declaration: int a[5][4];
100
[065] a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] A[2][0] a[2][1]
0 0 0 0 0 0 0 0 0 0
[100] [102] [104] [106] [108] [110] [112] [114] [116] [118]
a[2][2] a[2][3] a[3][0] A[3][1] A[3][2] a[3][3] A[4][0] A[4][1] a[4][2] A[4][3]
0 0 0 0 0 0 0 0 0 0
13
[120] [122] [124] [126] [128] [130] [132] [134] [136] [138]
Arrays
Unsized array initializations are not restricted to one-
dimensional arrays
For multidimensional arrays, one must specify all but
the leftmost dimension for C to index the array properly
With this approach, one can build tables of varying length
and the compiler will automatically allocate enough storage
For example:
int array1 [ ] [2] [3] [5]; // valid declaration
int array2
y [ ] [ ] [3]
[ ] [5];
[ ] // invalid declaration
14
Arrays
To access a particular element (variable) of an array, one
must specify an index = [[element_number
element_number – 1], for example,
if an arra
array is declared as
as: int
i t books_in_stock
b k i
books_in_stock[10];
t k[10]
k[10];
[10] then
th
books_in_stock [2] indicates the third variable of the array
During the declaration of an array,
array the square brackets must
contain a valid integer constant (or constant variable
defined by a macro)
O the
On th other
th hand,
h d whenh accessing i a specific
ifi array element
l t
in anywhere in the program, the index inside the square
bracket mayy be an integer
g constant,, variable or expression.
p
However, The Integer expressions/variables can be used
within the square brackets to access an element only if
they evaluate to a valid integer index
15