C array
Description
An array is a data structure process multiple elements with the same data type. Array elements are accessed using subscript. The valid range of subscript is 0 to size -1.
Syntax
type arrayName[length];
Example - Array creation and assignment
#include <stdio.h>
main()/*from w w w .ja v a2s.com*/
{
int a[5];
int i;
for(i = 0;i<5;i++)
{
a[i]=i;
}
for(i = 0;i<5;i++)
{
printf("value in array %d\n",a[i]);
}
}
The code above generates the following result.
Example - Array memory
Array elements occupy consecutive memory locations.
#include <stdio.h>
// w ww .j a v a 2s . c om
main()
{
int a[5];
int i;
for(i = 0;i<5;i++)
{
a[i]=i;
}
for(i = 0;i<5;i++)
{
printf("value in array %d\n",a[i]);
}
for(i = 0;i<5;i++)
{
printf("value in array %d and address is %16lu\n",a[i],&a[i]);
}
}
The code above generates the following result.