[Univ of Cambridge] [Dept of Engineering]
next up previous contents
Next: Memory Allocation Up: Keywords, Operators and Declarations Previous: Bit operations

Declarations

First, a note on terminology. A variable is defined when it is created, and space is made for it. A variable is declared when it already exists but needs to be re-described to the compiler (perhaps because it was defined in another source file). Think of declaring in C like declaring at customs - admitting to the existence of something.

C declarations are not easy to read. Any good book on C should explain how to read complicated C declarations ``inside out'' to understand them, starting at the variable name and working outwards back to the base type. You shouldn't need to use complicated declarations so don't worry too much if you can't `decode' them. Keep a cribsheet of useful typedefs and play with cdecl (see section 16.1).

ANSI C introduced the use of the `void' keyword in various contexts.

The following examples show common declarations.

int *p pointer to an int
int x[10] an array of 10 ints
int (*x)[10] a pointer to an array of 10 ints
int *x[10] array of 10 pointers to ints
int (*f)(int) pointer to a function taking and returning an int
void (*f)(void) pointer to a function taking no args and returning nothing
int (*f[])(int) An array of pointers to a functions taking and returning an int

Note the importance of the brackets in these declarations. If a declaration gets too complex it should be broken down. For example, the last example could be rewritten as

typedef int (*PFI)(int) /* declare PFI as pointer to function that 
                           takes and returns an int.*/
PFI f[];

next up previous contents
Next: Memory Allocation Up: Keywords, Operators and Declarations Previous: Bit operations
Tim Love
1999-10-06