Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!unido!opal!tmpmbx!netmbx!acunger From: acunger@netmbx.UUCP (Arnie Unger) Newsgroups: comp.os.msdos.programmer Subject: Re: QuickC compiler bug with 2D array? Message-ID: <1989@netmbx.UUCP> Date: 19 Feb 91 23:04:41 GMT References: <1991Feb19.062220.26093@agate.berkeley.edu> Distribution: comp.os.msdos.programmer; alt.msdos.programmer; comp.lang.c Organization: netmbx, Berlin, West Germany Lines: 42 bmyers@garnet.berkeley.edu (Brian Myers) writes: >The following code was excerpted from a longer program for constructing >crossword puzzles. This short program does not work because each >assignment statement seems inexplicably to assign a value to *two* >different array elements at the same time. I observed this double >action with the "Watch Value" feature on the Debug menu. >typedef struct puzzlecell { /* defines one cell in a 15x15 */ > char letter; /* crossword puzzle grid */ > int number; >} CELL; >CELL grid[14][14]; /* crossword puzzle grid 15x15 */ /********************************************************************* Your problem is right here, and it is an easy one. If you allocate a 14 X 14 grid, you only get valid addresses of 0 to 13 (an easy error to make) Try allocating it as 15 X 15 and you can use grid[10][14].letter with no error. By poking into grid[10][14], you were probably in the memory location for grid[11][0], or so... **********************************************************************/ >void main (void) >{ > /* each line _should_ write a Z in one square of the grid. */ > /* each line _does_ write a Z in _two_ squares. */ > grid[4][0].letter = 'Z'; /* also puts 'Z' at grid[3][14].letter */ > grid[10][14].letter = 'Z'; /* also puts 'Z' at grid[11][0].letter */ >} >/* A similar problem occurs whenever the second index is 0 or 14. */ >/* When the second index is > 0 and < 14, no problems occur. */ >--------------------------------------------------------------------------- >Brian Myers bmyers@garnet.berkeley.edu Note the change with the declaration of grid as 15 X 15 vice 14 X 14. You were one address short. With 15 X 15, your valid addresses are 0 to 14. Arnie Unger (acunger%netmbx@tmpmbx.UUCP) Berlin, Germany --- Seen on a F-111 bomb: TO ANY IRAQI SERVICEMEMBER!