We can give a formal definition of array. An array is simply a 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, or salaries of 300 employee or ages of 50 employees. Thus an array is a collection of similar elements. These similar elements could be all ints, or all floats or all chars etc. usually, the array of characters is called a ‘string’, where as an array of ints 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
Standard array declaration is as
type name[lengthofarray];
We can declare the array of any type
double height[10];
float width[20];
int min[9];
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
For example we can declare an array of 5 integers as
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. 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 5 elements of type int.
int myArray[5] = {1, 2, 3, 4, 5};
Performing Operations on Arrays
Here is a program that will demonstrate the simple operations of the array.
#include <stdio.h>
#define N 10
void oneWay(void);
void anotherWay(void);
int main(void) {
printf("\noneWay:\n");
oneWay();
printf("\nantherWay:\n");
anotherWay();
}
/*Array initialized with aggregate */
void oneWay(void) {
int vect[N] = {1,2,3,4,5,6,7,8,9,0};
int i;
for (i=0; i<N; i++)
printf("i = %2d vect[i] = %2d\n", i, vect[i]);
}
/*Array initialized with loop */
void anotherWay(void) {
int vect[N];
int i;
for (i=0; i<N; i++) vect[i] = i+1;
for (i=0; i<N; i++) printf("i = %2d vect[i] = %2d\n", i, vect[i]);
}
/* The output of this program is
oneWay:
i = 0 vect[i] = 1
i = 1 vect[i] = 2
i = 2 vect[i] = 3
i = 3 vect[i] = 4
i = 4 vect[i] = 5
i = 5 vect[i] = 6
i = 6 vect[i] = 7
i = 7 vect[i] = 8
i = 8 vect[i] = 9
i = 9 vect[i] = 0
antherWay:
i = 0 vect[i] = 1
i = 1 vect[i] = 2
i = 2 vect[i] = 3
i = 3 vect[i] = 4
i = 4 vect[i] = 5
i = 5 vect[i] = 6
i = 6 vect[i] = 7
i = 7 vect[i] = 8
i = 8 vect[i] = 9
i = 9 vect[i] = 10
*/
Here is a more complex program that will demonstrate how to read/write/traverse the integer arrays
#include <stdio.h>
#define NMAX 10
void intSwap(int *x, int *y);
int getIntArray(int a[], int nmax, int sentinel);
void printIntArray(int a[], int n);
void reverseIntArray(int a[], int n);
int main(void) {
int x[NMAX];
int hmny;
hmny = getIntArray(x, NMAX, 0);
printf("The array was: \n");
printIntArray(x,hmny);
reverseIntArray(x,hmny);
printf("after reverse it is:\n");
printIntArray(x,hmny);
}
void intSwap(int *x, int *y)
/* It swaps the content of x and y */
{
int temp = *x;
*x = *y;
*y = temp;
}
void printIntArray(int a[], int n)
/* n is the number of elements in the array a.
* These values are printed out, five per line. */
{
int i;
for (i=0; i<n; ){
printf("\t%d ", a[i++]);
if (i%5==0)
printf("\n");
}
printf("\n");
}
int getIntArray(int a[], int nmax, int sentinel)
/* It reads up to nmax integers and stores then in a; sentinel
* terminates input. */
{
int n = 0;
int temp;
do {
printf("Enter integer [%d to terminate] : ", sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full\n");
else
a[n++] = temp;
}while (1);
return n;
}
void reverseIntArray(int a[], int n)
/* It reverse the order of the first n elements of a */
{
int i;
for(i=0;i<n/2;i++){
intSwap(&a[i],&a[n-i-1]);
}
}