|
|||
Department of Engineering | |
University of Cambridge > Engineering Department > computing help |
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[];