TypeofArray nameOfArray [theSizeOfArray];
For instance,
int studentMark [10];
The above array is a declaration of an array of type integer. The name of the array is studentMark, and the array is of size 10.
Arrays in C can be initialized by using a single statement that declares the array or by breaking it into two statements. In the first instance, the above array will appear as follows:
int studentMark [10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 99};
Alternatively,
int studentMark [10];
studentMark [0] =10;
studentMark [1] =20;
An example illustrating all of the concepts above is as follows,
/* program that prints out five numbers */
#include
int main ()
{
int numbers[5];
int m, n;
/*initializing array elements */
for (m =0; m<= 5; m++){
numbers[m] =m;
}
/*outputting the values in an array */
for (n =0; n <=5; n++)
{
printf ("Number [%d] =%dn", n, numbers[n]);
}
return 0;
}
}
The output from the code above appears below,
Number [0] =1;
Number [0] =2;
Number [0] =3;
Number [0] =4;
Number [0] =5;
Did you intend for your array to over write the values, it looks as though it wrote all of the numbers to the same element [0], or am I reading it wrong, which is highly possible.