Arrays of any type can be formed in C. The syntax is simple:
type name[dim]; In C, arrays starts at position 0. The elements of the array occupy adjacent locations in memory. C treats the name of the array as if it were a pointer to the first element--this is important in understanding how to do arithmetic with arrays. Thus, if v is an array, *v is the same thing as v[0], *(v+1) is the same thing as v[1], and so on:
Consider the following code, which illustrates the use of pointers:
#define SIZE 3 void main(){ float x[SIZE]; float *fp; int i; /* initialize the array x */ /* use a "cast" to force i */ /* into the equivalent float */ for (i = 0; i <> x[i] = 0.5*(float)i; /* print x */ for (i = 0; i <> printf(" %d %f \n", i, x[i]); /* make fp point to array x */ fp = x; /* print via pointer arithmetic */ /* members of x are adjacent to */ /* each other in memory */ /* *(fp+i) refers to content of */ /* memory location (fp+i) or x[i] */ for (i = 0; i <> printf(" %d %f \n", i, *(fp+i));}
(The expression ``i++'' is C shorthand for ``i = i + 1''.) Since x[i] means the i-th element of the array x, and fp = x points to the start of the x array, then *(fp+i) is the content of the memory address i locations beyond fp, that is, x[i].
