C99 Seg fault on while(), why ?


Joined
Sep 13, 2022
Messages
1
Reaction score
0
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.

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
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top