Out of bounds 2D array error in C -


im stuck on 1 part , hoping help. have project word search. program reads in file contains rows , columns followed word search puzzle itself. required create possible combinations of strings word search , check combinations dictionary provided text document.

here's example of file read in 1st rows , 2nd cols followed word search puzzle:

4 4 syrt gtrp faaq pmrc 

so have been able of code work except function creates strings above file. needs search wordsearch , create strings, each created string gets passed on function check if it's in dictionary. code keeps going out of bounds when creating strings, , it's continuing cause seg faults frustrating.

theses constants declared, every possible direction go while searching word search puzzle possible string combinations

const int dx_size = 8; const int dx[] = {-1,-1,-1,0,0,1,1,1}; const int dy[] = {-1,0,1,-1,1,-1,0,1}; 

this function have create strings:

int strcreate(char** puzzle, char** dictionary, int n, int rows, int col){  int x, y; int nextx, nexty, i; char str[20] = {0}; int length = 1;  for(x = 0; x < rows; x++)   {      for(y = 0; y < col; y++)      {          //grabs base letter         str[0] = puzzle[x][y];         length = 1;         for(i = 0; < dx_size; i++)          {            while(length < max_word_size)           {               nextx = x + dx[i]*length;              nexty = y + dy[i]*length;               // checking bounds of next array             //this i'm having trouble.              if((x + nextx) < 0 || (nextx + x) > (col-1)){                 printf("out of bounds\n");                 break;             }              if((y + nexty) < 0 || (nexty + y) > (rows-1)){                 printf("out of bounds\n");                 break;             }                str[length] = puzzle[nextx][nexty];               //search str in dictionary             checkstr(str, dictionary, n);             length++;              }             memset(&str[1], '\0', 19);          }       }    } return 0; } 

i know i'm not checking bounds can't figure out how to. when x = 1 , nextx = -1, passes bounds check, array @ puzzle[0][0] nextx put puzzle[-1][0] out of bounds causing seg fault.

thank taking time read, , appreciate @ all.

nextx , nexty indices used access array puzzle. array bound check should include same. array bound check includes example x+nextx.

        // checking bounds of next array         //this i'm having trouble.          if((x + nextx) < 0 || (nextx + x) > (col-1)){             printf("out of bounds\n");             break;         } 

example: if( nextx < 0) printf("out of bounds...\n");


Comments