gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:49:13 25 4
gpt4 key购买 nike

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

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

这是我的代码。

  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 != '\0')
{

// 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 == '\0'))
{
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 != '\0'){
word++;

}
if (*word == '\0'){
break;
}
word++;
// print_char(' ');

}
row++; //next grid row
}
grid_clm++; //next grid column
}

最佳答案

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

关于C 运行时错误 "Segmentation Fault"- 如何删除它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58415225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com