gpt4 book ai didi

c - 使用 C 的顺序搜索和释放数组

转载 作者:太空宇宙 更新时间:2023-11-04 07:29:36 25 4
gpt4 key购买 nike

所以我尝试使用顺序搜索来检查一个字符串在我的数组中出现了多少次。在我的程序中,我要求用户选择他们希望打开和处理的文件。

void search(char **table, int **frequency, int wordSize)
{
// Local Declaration
int i, j;
int count = 1;
char target[25];

// Statement
for(i = 0; i < wordSize; i++)
{
if(table[i] != NULL)
{
strcpy(target, table[i]);
for(j = i + 1; j < wordSize; j++)
{
if(strcmp(target, table[j]) == 0 && target != table[i])
{
count++;
free(table[j]);
table[j] = NULL;
}
}
}
count = 1;
}

return;
}

所以在这两个文件中,其中一个打开并处理没有任何问题,但是当我尝试打开第二个文件时它崩溃了。我试图了解导致我的程序崩溃的原因,因为这两个文件都只包含字符串并且没有一个字符串超过 24 个字符。

最佳答案

if(table[j] != NULL && strcmp(target, table[j]) == 0 && target != table[i])

您可能正在访问 table 变量,您在上一次迭代中将其设置为 NULL

关于c - 使用 C 的顺序搜索和释放数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15080716/

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