繁体   English   中英

分段故障:11 c结构

[英]Segmentation fault: 11 c structures

我有一个结构记录数组和一个函数insertinsert or update记录。

insert函数接受list (记录数组), name (书名), authoryearcopieslist大小n

如果找到记录,它将更新记录,否则插入新记录 在这里n=7

void insert(struct books *list,char name[],char author[],int year,int copies,int n)
{
    int i,found=0,empty;

    for(i=0;(i<n) && (found==0);i++)
    {
        // update works fine
        if( strcmp(name,list[i].name)==0 && strcmp(author,list[i].author)==0 )
        {
            list[i].copies=copies;
            list[i].year=year;
            printf("\n\n####################################################\n");
            printf("####\tRecord was successfully updated!\t####\n");
            printf("####################################################\n");
            found=1;
        }
        //get an empty record
        if(strcmp(list[i].author,"i")==0){empty=i;}
    }

    //insert gives segmentation error
    if(found==0)
    {
        strcpy(list[empty].name,name);
        strcpy(list[empty].author,author);
        list[empty].year=year;
        list[empty].copies=copies;

        printf("\n\n####################################################\n");
        printf("####\tRecord was successfully inserted!\t####\n");
        printf("####################################################\n");
    }
}  

我的list数组是:

A
Ruby On Rails
2004
100
B
Inferno
1993
453
C
Harry Potter and the soccers stones
2012
150
D
Harry Potter and the soccers stone
2012
150
E
Learn Python Easy Way
1967
100
F
Ruby On Rails
2004
130
i
i
0
0  

为什么会出现Segmentation error: 11

可能您需要初始化empty

空= 0;

实际上,SO不是调试服务。 所以不要再问这样的问题了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM