中止陷阱:C中有6个错误

时间:2015-04-22 15:22:50

标签: c loops pointers

我有这段代码:

#include<stdio.h>
#include<string.h>

unsigned long hash(char *str)
{
    unsigned long hash = 5381;
    int c;

    while ((c = *str++))
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

    return hash;
}
int main()
{

    char s[50],ans[50];

    int max_freq = 0,num=0,hash_value,i;
    unsigned long int a[10000],b[10000];


    FILE *fp = fopen("/Users/Brikman23/Desktop/input.txt","r"); //remember to specify correct path of input file..

    if (fp==NULL)
    {
        printf("Error: Input File not found..");
        return -1;
    }

    while(fscanf(fp,"%s",s)==1)
    {
        hash_value = hash(s);
        for(i=0;i<num;i++)
        if(a[i]==hash_value)
        break;

        if(i==num)
        {
            b[num]=1;
            a[num]=hash_value;

            if(b[num]>max_freq)
            {
                max_freq = b[num];
                strcpy(ans,s);

            }   

            num++;  
        }
        else
        {
            b[i]++;
            if(b[i]>max_freq)
            {
                max_freq = b[i];
                strcpy(ans,s);

            }   

        }   


    }

    printf("The word with the highest number of appearances is: %s",ans);
    printf("\nNumber of appearances: %d",max_freq);

    return 0;

}

当我使用gcc编译它时,它不会给我一个错误,但输出总是给我Abort trap:6而不是它应该给我:

出现次数最多的单词是:

出场次数:

我看过其他帖子,但他们没有帮助。任何帮助将不胜感激。

0 个答案:

没有答案