C ARRAYS
The C language provides a capability that enables the user to
define a set of ordered data items known as an array.
What
is an Array in C Language?
An array in C
Programing Language can be defined as number of memory
locations, each of which can store the same data type and which
can be references through the same variable name.
An array
is a collective name given to a group of similar quantities.
These similar quantities could be percentage marks of 100
students, number of chairs in home, or salaries of 300 employees
or ages of 25 students. Thus an array is a collection of similar
elements. These similar elements could be all integers or all
floats or all characters etc. Usually, the array of characters
is called a “string”, where as an array of integers or floats is
called simply an array. All elements of any given array must be
of the same type i.e we can’t have an array of 10 numbers, of
which 5 are ints and 5 are floats.
Declaration of an
Array
Arrays must be declared before they can be
used in the program. Standard array declaration is as
type
variable_name[lengthofarray];
Here type
specifies the variable type of the element which is going to be
stored in the array. In C programmin language we can declare the
array of any basic standard type which C language supports. For
example
double height[10]; float width[20]; int min[9]; char name[20];
In C Language, arrays starts at position 0. The elements of
the array occupy adjacent locations in memory. C Language treats
the name of the array as if it were a pointer to the first
element This is important in understanding how to do arithmetic
with arrays. Any item in the array can be accessed through its
index, and it can be accesed any where from with in the program.
So
m=height[0];
variable m will have the
value of first item of array height.
The declaration int
values[10]; would reserve enough space for an array called
values that could hold up to 10 integers. Refer to the below
given picture to conceptualize the reserved storage space.
values[0] | CONTINOUS MEMORY LOCATIONS | |
values[1] | ||
values[2] | ||
values[3] | ||
values[4] | ||
values[5] | ||
values[6] | ||
values[7] | ||
values[8] | ||
values[9] | ||
The program below will declare an array of five integers and print all the elements of the array.
int myArray[5] = {1,2,3,4,5}; /* To print all the elements of the array for (int i=0;i<5;i++){ printf("%d", myArray[i]); }
Initializing Arrays:
Initializing of array is very simple in c programming. The
initializing values are enclosed within the curly braces in the
declaration and placed following an equal sign after the array
name. Here is an example which declares and initializes an array
of five elements of type int. Array can also be initialized
after declaration. Look at the following C code which
demonstrate the declaration and initialization of an array.
int myArray[5] = {1, 2, 3, 4, 5}; //declare and initialize the array in one statement int studentAge[4]; studentAge[0]=14; studentAge[1]=13; studentAge[2]=15; studentAge[3]=16;
To summarize, arrays provides a simple mechanism where more than
one elements of same type are to be used. We can maintain,
manipulate and store multiple elements of same type in one array
variable and access them through index.