next up previous contents index
Next: Loops Up: Variables Previous: Scalar Variables   Contents   Index


Arrays in C are declared as in
  int scores[100];
  char ans = 'y';
As in Perl, array indices start at 0, and individual array elements can be accessed through this index (eg, scores[12]). If an assignment of the values is done at the time of declaration, the arrays may be declared as
  int a[] = {1,2,3,4,5};
  char name[] = "Jessica";
Higher-dimensional arrays can be declared as
  int mat[100][300];

Note that in C (at this level) the size of the array must be known at the time it is declared - one cannot dynamically declare an array with a simple declaration as this (we shall see later, when we discuss pointers, how this can be done).

If one is testing out something and wants to alter the size of an array, it can be inconvenient to adjust the source code and then recompile. One method one might use to do this is through the use of #define directives:

#include <stdio.h>
#ifndef N
#define N 100

int main(void) {
  int scores[N];
  return 0;
The #define N 100 statement defines the character N to be 100 - N here is not a variable. The #ifndef ... #endif block has the effect of setting N to be 100 if N is not defined already. One can define N at the time the program is compiled through the use of a -D flag:
   bash$ gcc -o hello hello.c -DN=300
Thus, if the program is compiled with N defined, the given value will be used; otherwise, a default value of 100 will be used.

At some point or another everyone is bound to try to access an array element that is outside of the bounds of the declared size of the array (for example, scores[45] when scores was declared as scores[10]). What happens in such cases is unpredictable - you may get an (apparently) random value for the element, or your program may crash with a somewhat scary-sounding error message. What happens here is the following - when you declare an array, the compiler asks the system to set aside exactly enough memory to hold the number (and types) of elements requested. Trying to access an element outside of the declared bounds effectively tries to access an area of memory that your program has not reserved. If this area is free, you may just get some seemingly random value coming from what was previously stored in this area, but more seriously, if this area is being used elsewhere, you may start to interfere with other programs. The morale of this is that you have to be very careful to stay within the bounds of declared array sizes - part of the reason C is so fast and efficient is that it uses the minimum amount of memory necessary, but that comes at a cost of stricter programmer care.

There are a number of functions available to help with manipulations of strings (arrays of characters):

In all cases the strings used must previously have been declared of sufficient size in order to carry out the requested operations.
next up previous contents index
Next: Loops Up: Variables Previous: Scalar Variables   Contents   Index