366 Intermediate C Programming
#d ef in e INV A LIDSYM BOL ’-’8
typedef s t ru ct9
{10
int numRow , numCol ; // size of the maze11
int startRow , startCol ; // sta r ting locatio n12
int exitRow , exitCol ; // exit location13
int curRow , curCol ; // current location14
// brick ? exit ? starting point ? corridor ? 2 - dim ensional15
// array storing the cells16
int * * cells ;17
} Maze ;18
// directions , ORIGIN marks the starting point19
enum { ORIGIN , EAST , SOUTH , WEST , NORTH };20
// move forward , backward , or found exit alread21
enum { FORWARD , BACKWARD , DONE };22
// read the maze from a file23
Maze * Maze _const r uct ( char * fi l eName );24
// release memory before the program ends25
void Maze_ destruc t ( Maze * mz ) ;26
// print the maze ’s prope rties ( mainly for debu g ging )27
void Maze_pri n t ( Maze * mz ) ;28
#e ndif29
The following listing gives sample implementations for the functions declared in the
header file.
// mazerea d . c1
#in clude " maze .h "2
#in clude < stdio .h >3
#in clude < stdlib .h >4
// A static fu n c tion can be called by another funct i o n5
// in the same file . A static function cannot be called6
// by any function outside this file .7
// If ptr is NULL , print an error message and exit8
s t a t i c void checkMal l oc ( void * ptr , char * mes s a g e );9
// find the length of a line in a file ( EOF or ’ n ’)10
s t a t i c i n t fin dLineL ength ( FILE * fh ) ;11
// Find the numbers of rows and columns . If the maze is not12
// rectangular , use the widest row13
s t a t i c void Maze_f i ndSize ( FILE * fh , in t * numRow ,14
int * numCol );15
s t a t i c void checkMal l oc ( void * ptr , char * mes s a g e )16
{17
i f ( ptr == NULL ) // malloc fail18
{19
printf (" malloc for %s fail n " , message ) ;20
}21
}22
s t a t i c i n t fin dLineL ength ( FILE * fh )23
{24
int ch ;25
int length = 0;26