Previous | Home | Next |
The elements of an array can be accessed easily by providing its index within [] with the identifier and can modify or read its value.
34 | 78 | 98 | 45 | 56 |
The format of accessing the array element is simple as arr[index]. To fetch a value from the above array which is placed in the position 3 we can use the following syntax :
To write a value in an empty we can use the following syntax :
That's how we can access the elements of an array.
Also consider that it is syntactically incorrect to exceed the valid range of indices for an array. If you do so, accessing the elements out of bounds of an array will not cause any compilation error but would result in the runtime error and it can lead to hang up your system.
A simple program using array
#include<conio.h>
void main()
{
int arr[5] = {1,2,3,4,5}; /*declaring and initializing the array */
int sum=0, i;
for(i=0;i<5;i++)
{
sum=sum+arr[i]; /*adding the individual elements of the array*/
}
printf("The value of the sum of all elements of the array is : %d", sum);
getch();
}
Output : The value of the sum of all elements of the array is : 15
Previous | Home | Next |