Introduction To An Array
Introduction To An Array
1. One-dimensional arrays
2. Multidimensional arrays
Declaration of one-dimensional array
data_type array_name[array_size];
For example:
Here, the name of array is age. The size of array is 5,i.e., there are 5
items(elements) of array age. All element in an array are of the same
type (int, in this case).
Array elements
Size of array defines the number of elements in an array. Each element
of array can be accessed and used by user according to the need of
program. For example:
int age[5];
Here, the size of array age is 5 times the size of int because there are 5
elements.
Suppose, the starting address of age[0] is 2120d and the size of int be 4
bytes. Then, the next address (address of a[1]) will be 2124d, address
of a[2] will be 2128d and so on.
int age[5]={2,4,34,3,4};
int age[]={2,4,34,3,4};
In this case, the compiler determines the size of array by calculating the
number of elements of an array.
For example:
#include <stdio.h>
main(){
int marks[10],i,n,sum=0;
printf("Enter number of students: ");
scanf("%d",&n);
for(i=0;i<n;++i){
printf("Enter marks of student%d: ",i+1);
scanf("%d",&marks[i]);
sum+=marks[i];
}
printf("Sum= %d",sum);
Output
sum=45
For example: arr[10].