C 运行时错误"Segmentation Fault" - 如何删除它?



我不确定为什么我的代码中出现分段错误。但是,我知道它发生在while(*word == *grid_char)块内。我还尝试删除其中的代码,看看会发生什么,但我仍然收到分段错误错误。但是,如果我完全删除该 while 循环,错误就会消失。

我知道什么是seg错误,但我不明白为什么在这种情况下会出现错误。我也不知道如何调试它。

这是我的代码。

char grid[(MAX_DIM_SIZE + 1) * MAX_DIM_SIZE + 1];
char dictionary[MAX_DICTIONARY_WORDS * (MAX_WORD_SIZE + 1 ) + 1 ];
int dictionary_idx[MAX_DICTIONARY_WORDS];
char *grid_char;
char *match_word;
int grid_idx = 0;
char *word = dictionary + dictionary_idx[0];
int word_found = 0;
int max_grid_height = row; // max y (max_grid_height) on grid
int row = 0;
int grid_clm = 0;
// print_char(grid[18]);
while (grid[grid_clm] != 'n')
{
row = 0;
while(row < max_grid_height)
{
word = dictionary + dictionary_idx[0]; //first char from dictionary ***

while(*word != '')
{
// print_char(*word);
int match_row = row; // back to start point grid - using this to compare potential match
match_word = word; // back to start point dict - using this to compare potential match
grid_char = grid[grid_clm + max_grid_length*match_row];
print_char(grid_char);
print_char(*word);
print_char(*(match_word+1));
print_char(' ');
while(*word == *grid_char)
{
match_row++;
match_word++;
if ((*match_word == 'n' || *match_word == ''))
{
print_char('F');
continue;
}
if(match_row >= max_grid_height){
break;
}
grid_char = grid[grid_clm + max_grid_length*match_row];
}
// next dictionary word
while(*word != 'n' && *word != ''){
word++;
}
if (*word == ''){
break;
}
word++;
// print_char(' ');
}
row++; //next grid row
}
grid_clm++; //next grid column
}

我认为问题出在 char 指针grid_char初始化时,您正在分配一个 char 值而不是内存地址。

相关内容

  • 没有找到相关文章

最新更新