I have a programming project for class where I'm writing bank software. One of the modules I have is I'm trying to close an account. I keep getting a segmentation error.
Assuming linearSearch works and returns the index of the account but you dont use it, The for loop appears to be just copying the first record into the rest of the array. This newbankRecord[i+1] is causing the seg fault.
The linearSearch does work I've used it in other functions. I'm guessing that's what the problem is too. How do I fix it? Oh and I edited the second part. I it's bankRecord[i] not newbankRecord[i]. my bad
you havent given i a value, so the size of newbankRecord is undefined. Here newbankRecord[i+1] you are reading past the size of the array causing the seg fault. For the copying I would do something like
1 2 3 4 5 6 7 8 9 10 11 12 13 14
AccountInfo newbankRecord[numAccs - 1];
//...
int i =0, k = 0;
if ((i = linearSearch(acno, bankRecord, numAccs)) >0)
{
for(int j = 0; i < numAccs;++j)
{
if (i != j)
{
newbankRecord[k] = bankRecord[j]
++k;
}
}
}
This is ugly and untested, but should give you an idea.