Accessing Elements of Array

  1. We all know that array elements are randomly accessed using the subscript variable.
  2. Array can be accessed using array-name and subscript variable written inside pair of square brackets [].
Consider the below example of an array –
accessing array
In this example we will be accessing array like this –
arr[3]  = Third Element of Array
arr[5]  = Fifth Element of Array
arr[8]  = Eighth Element of Array
whereas elements are assigned to an array using below way –
arr[0]  = 51
arr[1]  = 32
arr[2]  = 43
arr[3]  = 24
arr[4]  = 5
arr[5]  = 26

Program #1 : Accessing array

#include<stdio.h>
#include<conio.h>

void main()
{
int arr[] = {51,32,43,24,5,26};
int i;
    for(i=0; i<=5; i++) {
         printf("\nElement at arr[%d] is %d",i,arr[i]);
 }
getch();
}
Output :
Element at arr[0] is 51
Element at arr[1] is 32
Element at arr[2] is 43
Element at arr[3] is 24
Element at arr[4] is 5
Element at arr[5] is 26

No comments:

Post a Comment