代码块出错并显示在线编译中的分段错误?

时间:2014-02-03 11:33:42

标签: c segmentation-fault codeblocks

以下代码导致代码块停止。它显示为导致程序关闭的一些问题。当我尝试在线编译时,它显示为分段错误(核心转储)。我无法找出它是什么!

//Hash table

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

int main()
{
    int option,i,key;
    char value[10], hashtab[10][10];

    for(i=0;i<10;i++)
    {
        strcpy(hashtab[i],'\0');
    }

    printf("\nEnter 1-Insert ANY-Exit");
    scanf("%d",&option);
    while(option==1)
    {
        printf("\nEnter the Value: ");
        scanf("%s",value);
        key=keygen(value);
        strcpy(hashtab[key],value);
     }

    for(i=0;option==2&&i<10;i++)
    {
        printf("\n%s",hashtab[i]);
    }
}

int keygen(char *value)
{
    int i,key=0;
    for(i=0;i<strlen(value);i++)
    {
        key=key+value[i];
    }
    return key;
}

1 个答案:

答案 0 :(得分:1)

strcpy循环中使用的

for应该是如下参数,

  char *strcpy(char *dest, const char *src);

理想情况下,传递的第二个参数应该是src指向的字符串。

实现目标,strcpy(hashtab[i], "\0");应该有效。