ARRAY IN C
Arrays is a data structure that can store similar type of data. IN other words an array is a collection of homogenous data (same type of data), which are stored in contiguous memory locations. In an array the first element goes to the lowest index and the last element to the highest index.
Declaring Array
Below is the syntax for declaring an array
type array name[array size];
for eg:- int Number[10];
Initializing Array
we can initialize an array one by one or simply in one statement like given below.
int Number[10] = {12,34,43,56,,23,43,65,09,87};
Accessing Array Elements
we can access an array by indexing the array name. we can do this by simply puting the index of the element within square brackets after the name of the array.
For example
int name = Number[9];
The above statement will take the 10th element from the array and assign the value to name variable .
Below is the example which uses all three above mentioned concepts of an array .
int main ()
{
int N[ 5 ]; /* n is an array of 5 integers */
int i,j;
/* initialize elements of array n to 0 */
for ( i = 0; i < 5; i++ )
{
N[ i ] = i + 100; /* set element at location i to i + 100 */
}
/* output each array element's value */
for (j = 0; j < 5; j++ )
{
printf("rollno[%d] = %d\n", j, N[j] );
}
return 0;
}
**OUTPUT**
rollno[0] = 100
rollno[1] = 101
rollno[2] = 102
rollno[3] = 103
rollno[4] = 104
0 Comment(s)