Reputation:
I have an array of structure records and a function insert
to insert or update
the records.
The insert
function take list
(an array of records),name
(book name),author
, year
,copies
and size n
of the list
It updates the record if it find's one otherwise inserts a new one. here 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");
}
}
My list
array is:
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
Why is it giving Segmentation error: 11
?
Upvotes: 0
Views: 259
Reputation: 2570
Probably you need to initialize empty
. Do
empty=0;
And really, SO is not a debugging service. So stop asking questions like this.
Upvotes: 3