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

时间:2013-02-26 03:12:53

标签: c

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

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个字符。

1 个答案:

答案 0 :(得分:3)

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

您可以访问您在上一次迭代中table编辑的NULL变量。