An array is a collection of objects of the same data type. The subscripting operator ([]) provides the mechanics for creating an index to array elements. This form of access is called indexing or subscripting. An array facilitates the coding of repetitive tasks by allowing the statements executed on each element to be put into a loop that iterates through each element in the array.
Consider the case where a programmer needs to keep track of a number of people within an organisation. So far, our initial attempt will be to create a specific variable for each user.
int name1 = 101;
int name2 = 232;
int name3 = 231;
This is okay, but what if you needed a thousand integers? An easier way is to declare an array of five integers
int a[5];
Example :
#include <stdio.h>main()
{
char word[20];word[0] = ‘H’;
word[1] = ‘e’;
word[2] = ‘l’;
word[3] = ‘l’;
word[4] = ‘o’;
word[5] = 0;
printf(”The contents of word[] is %s\n”, word );
}
Output:
The contents of word[] is HelloSome other Examples:
char str[16]=”Blueberry”;
Creates a string. The value at str[8] is the character “y”. The value at str[9] is the null character. The values from str[10] to str[15] are undefined.
char s[]=”abc”;
Dimensions the array to 4 (just long enough to hold the string plus a null character), and stores the string in the array.
int y[3]={4};
Sets the value of y[0] to 4 and y[1] and y[2] to 0.
