Hi,
I'm teaching myself C(99) and have developed a small telephone book program in answer to a text book question. I am using Code::Blocks (v 20.03, the latest version) for easy access to the debugger.
The offending code is the while() loop marked "Error here". As can be clearly seen, the while() test clause compares the value of a local unsigned int variable with the value pointed at by a dereferenced(!) passed in pointer to unsigned int. In the C::B Watch window, "index" has a displayed value of 0 (zero) whilst "*recordsUsed" has a displayed value of 0x0 (hex zero). Could some please point out why this while() statement is causing a seg fault ?
Mohram
I'm teaching myself C(99) and have developed a small telephone book program in answer to a text book question. I am using Code::Blocks (v 20.03, the latest version) for easy access to the debugger.
C:
void addRecord(record * theseRecords, record * aRecord,
unsigned int * recordsUsed)
{
unsigned int index = 0; /* Loop counter */
printf("\n\tAdding Record...\n");
/* Find first unused record */
while (index <= *(recordsUsed)) // <- Error here
{
if (theseRecords[index].recordUsed == 'Y')
index++;
} /* end while() */
strcpy(theseRecords[index].firstName, aRecord->firstName);
strcpy(theseRecords[index].lastName, aRecord->lastName);
strcpy(theseRecords[index].phoneNumber, aRecord->phoneNumber);
(*recordsUsed)++;
printf("\nDone\n");
} /* end addRecord() */
The offending code is the while() loop marked "Error here". As can be clearly seen, the while() test clause compares the value of a local unsigned int variable with the value pointed at by a dereferenced(!) passed in pointer to unsigned int. In the C::B Watch window, "index" has a displayed value of 0 (zero) whilst "*recordsUsed" has a displayed value of 0x0 (hex zero). Could some please point out why this while() statement is causing a seg fault ?
Mohram