Arrays
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:
Pointer use for an array
Consider the following code, which illustrates the use of pointers:
(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].
#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 < i =" 0;" fp =" x;" i =" 0;">
1. A First Program
2. Let's Compute
3. Loops
4. Symbolic Constants
5. Conditionals
6. Pointers
7. Arrays
8. Character Arrays
9. I/O Capabilities
10. Functions
11. Command-line Arguments
12. Graphical Interfaces: Dialog Boxes
No comments:
Post a Comment