MC logo
Array Echo
[^] Code Examples
<<Typedef Declaration arrecho1.c Basic Pointers>>
 * Read some integers into an array, then echo them.
 * Author: Tom Bennet
#include <stdio.h>
#define ARRMAX  100
        int arr[ARRMAX],        /* Array of numbers. */
            sub,                /* Subscript. */
            numin;              /* Number of integers read. */      

        /* Rad them in. */
        sub = 0;
        while(scanf("%d", &arr[sub]) == 1)
        numin = sub;

        /* Print them back out again. */
        for(sub = 0; sub < numin; ++sub)
                printf("%d\n", arr[sub]);

C arrays are like Java's in syntax, and that subscripts start from zero. The differences are really more substantial. Most important is that arrays are declared and allocated at the same time. There is no new needed to allocate. Furthermore, there are very few circumstances where you may omit the array size, like int arr[]. Generally, the brackets must contain a size, and that size has to be a constant. That gives the amount of space to allocate.
Reading: pp. 48-49
<<Typedef Declaration Basic Pointers>>