Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
enum { ARRAY_SIZE = 12 };
int disarray[ARRAY_SIZE];

These statements allocate storage for an array of 12 integers referenced by dis. Arrays are indexed from 0..n-1 (where n represents an array bound). Arrays can also be declared as follows:

Code Block
int itaarray[];

This is called an incomplete type because the size is unknown. If an array of unknown size is initialized, its size is determined by the largest indexed element with an explicit initializer. At the end of its initializer list, the array no longer has incomplete type.

Code Block
int itaarray[] = { 1, 2 };

While these declarations work fine when the size of the array is known at compilation time, it is not possible to declare an array in this fashion when the size can be determined only at runtime. The C99 standard adds support for variable-length arrays or arrays whose size is determined at runtime. Before the introduction of variable-length arrays in C99, however, these "arrays" were typically implemented as pointers to their respective element types allocated using malloc(), as shown in this example.

Code Block
int *datdis = (int *)malloc(ARRAY_SIZE * sizeof(int));

...

Code Block
for (i = 0; i < ARRAY_SIZE; i++) {
   disdat[i] = 42; /* Assigns 42 to each element; */
   /* ... */
}

...