ARRAYS in C
Vikas Chandra Sharma, Swarrnim School of Computing & IT, Swarrnim Startup and
Innovation University
Arrays are widely used data type in ‘C’ language. It is a collection of elements of similar data
type. These similar elements could be of all integers, all floats or all characters. An array of
character is called as string whereas and array of integer or float is simply called as an array.
So array may be defined as a group of elements that share a common name and that are
defined by position or index. The elements of an arrays are store in sequential order in
memory.
There are mainly two types of Arrays are used:
One dimensional Array
Multidimensional Array
One dimensional Array
So far, we’ve been declaring simple variables: the declaration
int I;
declares a single variable, named I, of type int. It is also possible to declare an array of
several elements. The declaration
int a[10];
declares an array, named a, consisting of ten elements, each of type int. Simply speaking, an
array is a variable that can hold more than one value. You specify which of the several values
you’re referring to at any given time by using a numeric subscript. (Arrays in programming
are similar to vectors or matrices in mathematics.) We can represent the array a above with a
picture like this:
In C, arrays are zero-based: the ten elements of a 10-element array are numbered from 0 to 9.
The subscript which specifies a single element of an array is simply an integer expression in
square brackets. The first element of the array is a[0], the second element is a[1], etc. You
can use these ``array subscript expressions’’ anywhere you can use the name of a simple
variable, for example:
a[0] = 10;
a[1] = 20;
a[2] = a[0] + a[1];
Notice that the subscripted array references (i.e. expressions such as a[0] and a[1]) can
appear on either side of the assignment operator. It is possible to initialize some or all
elements of an array when the array is defined. The syntax looks like this:
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
The list of values, enclosed in braces {}, separated by commas, provides the initial values for
successive elements of the array.
The subscript does not have to be a constant like 0 or 1; it can be any integral expression. For
example, it’s common to loop over all elements of an array:
int I;
for(I = 0; I < 10; I = I + 1)
a[i] = 0;
This loop sets all ten elements of the array a to 0.
Arrays are a real convenience for many problems, but there is not a lot that C will do with
them for you automatically. In particular, you can neither set all elements of an array at once
nor assign one array to another; both of the assignments
a = 0; /* WRONG */
and
int b[10];
b = a; /* WRONG */
are illegal.
To set all of the elements of an array to some value, you must do so one by one, as in the loop
example above. To copy the contents of one array to another, you must again do so one by
one:
int b[10];
for(I = 0; I < 10; I = I + 1)
b[i] = a[i];
Remember that for an array declared
int a[10];
there is no element a[10]; the topmost element is a[9]. This is one reason that zero-based
loops are also common in C. Note that the for loop
for(I = 0; I < 10; I = I + 1)
...
does just what you want in this case: it starts at 0, the number 10 suggests (correctly) that
it goes through 10 iterations, but the less-than comparison means that the last trip through
the loop has I set to 9. (The comparison I <= 9 would also work, but it would be less
clear and therefore poorer style.)
Multidimensional Array
The declaration of an array of arrays looks like this:
int a2[5][7];
You have to read complicated declarations like these ``inside out.’’ What this one says is that
a2 is an array of 5 something’s, and that each of the something’s is an array of 7 ints. More
briefly, ``a2 is an array of 5 arrays of 7 ints,’’ or, ``a2 is an array of array of int.’’ In the
declaration of a2, the brackets closest to the identifier a2 tell you what a2 first and foremost
is. That’s how you know it’s an array of 5 arrays of size 7, not the other way around. You can
think of a2 as having 5 ``rows’’ and 7 ``columns,’’ although this interpretation is not
mandatory. (You could also treat the ``first’’ or inner subscript as ``x’’ and the second as
``y.’’ Unless you’re doing something fancy, all you have to worry about is that the subscripts
when you access the array match those that you used when you declared it, as in the examples
below.)
To illustrate the use of multidimensional arrays, we might fill in the elements of the above
array a2 using this piece of code:
int I, j;
for(I = 0; I < 5; I = I + 1)
{
for(j = 0; j < 7; j = j + 1)
a2[i][j] = 10 * I + j;
}
This pair of nested loops sets a[1][2] to 12, a[4][1] to 41, etc. Since the first dimension of
a2 is 5, the first subscripting index variable, I, runs from 0 to 4. Similarly, the second
subscript varies from 0 to 6.
We could print a2 out (in a two-dimensional way, suggesting its structure) with a similar pair
of nested loops:
for (I = 0; I < 5; I = I + 1)
{
for (j = 0; j < 7; j = j + 1)
printf (“%d\t”, a2[i][j]);
printf (“\n”);
}
(The character \t in the printf string is the tab character.)
Just to see more clearly what’s going on, we could make the ``row’’ and ``column’’
subscripts explicit by printing them, too:
for(j = 0; j < 7; j = j + 1)
printf(“\t%d:”, j);
printf (“\n”);
for(I = 0; I < 5; I = I + 1)
{
printf(“%d:”, i);
for(j = 0; j < 7; j = j + 1)
printf(“\t%d”, a2[i][j]);
printf(“\n”);
}
This last fragment would print
0: 1: 2: 3: 4: 5: 6:
0: 0 1 2 3 4 5 6
1: 10 11 12 13 14 15 16
2: 20 21 22 23 24 25 26
3: 30 31 32 33 34 35 36
4: 40 41 42 43 44 45 46