首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >分段故障:11c结构

分段故障:11c结构
EN

Stack Overflow用户
提问于 2013-10-26 20:29:10
回答 1查看 256关注 0票数 0

我有一个结构记录数组和一个用于insert or update记录的函数insert or update

insert 函数采用 list**(an数组的记录)、**name (图书名)、**author**, year**,**copies 和大小 n E 232 list

如果它找到的记录不是就会插入一个新的,它就会更新记录。在这里n=7

代码语言:javascript
复制
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数组是:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-26 20:40:13

可能您需要初始化empty。做

empty=0;

实际上,并不是一个调试服务。所以别问这样的问题了。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19611558

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档