Accessing Arrays

For example:

    int grade[5];

    grades[0] refers to the first grade stored in the grades array.

    grades[4] refers to the fifth grade stored in the grades array.

 

grades

grades[0]

grades[1]

grades[2]

grades[3]

grades[4]

index [0] [1] [2] [3] [4]

 

Example : Accessing element 3

as variables

 

Indexed variables can be used anywhere that scalar variables are valid. 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.

Examples:

    grades[0]=98;

    grades[1]=grades[0]-23;

    grades[2]=( grades[0]  +  grades[1]  ) /2 ;

 

variable index

 

Any expression that evaluates to an integer may be used as a subscript .It don't have to be a constant.

For example if i and j are integer variables , the following subscripted variables are valid :

    grades[i]

    grades[i-j]

    grades[3*i]

 

The ith element of the array  is referred to as grades[i-1].

 

One advantage of using integer expressions as subscripts is that it allows sequencing through an array using a for loop . This makes a statements such as :

    total = grades[0] + grades[1] + grades[2] + grades[3] + grades[4]

 

unnecessary  and can be replaced by a for loop such as:

    for ( i=0 ; i<=4 ; i++)

         total = total + grades[i];

 

Here the variable i is used both as the counter in the for loop and as a subscript.

 

Input and output

 

 grades[4]=100;

    scanf(" %c %d %f ", &grades[2], &price[1] ,&code[0] );

 

    for( i=0; i<5 ; i++)

    { printf("Enter a grade");

       scanf("%d", &grades[0]); }

 

There is no single statement that can read or print all the elements of the array. You have to use a loop.

 

Maximum value example:

 

    maximum = price[0];    //assume initially that the first element is the maximum

    for ( i=1; i<=99; ++i)

       if (maximum < price[i])

          maximum = price[i];

 

Not Automatic

 

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

int a[10];  

                      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.)

 

Counter vs. size

 

In the little examples so far, we've always looped over all 10 elements of the sample array a. It's common, however, to use an array that's bigger than necessarily needed, and to use a second variable to keep track of how many elements of the array are currently in use. For example, we might have an integer variable

 

int c; /* number of elements of a[] in use */
 

Then, when we wanted to do something with a (such as print it out), the loop would run from 0 to c, not 10 (or whatever a's size was):

for(i = 0; i < c ; i = i + 1)
printf("%d\n", a[i]);

 

Naturally, we would have to ensure that c's value was always less than or equal to the number of elements actually declared in a.

 

Array Bounds

 

C has no array bounds checking to prevent the computer from referring to an element that does not exist.

 

For example , if you declare an array int students[30] and try to access students[35] there will be no error by the C compiler.