簡體   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