0
#include <stdio.h>
#include <string.h>
#include <string.h>
struct student
{
     int id;
     char name[30];
     float percentage;
};

int main()
{
     int i;
     struct student record[2];

     // 1st student's record
     record[0].id=1;
     strcpy(record[0].name, "Raju");
     record[0].percentage = 86.5;

     // 2nd student's record
     record[1].id=2;
     strcpy(record[1].name, "Surendren");
     record[1].percentage = 90.5;

     // 3rd student's record
     record[2].id=3;

     //strcpy(record[2].name, "Thiyagu");//--->Bug in this line

     //record[2].percentage = 81.5;//--->Bug in this line


     for(i=0; i<3; i++)
     {
         printf("     Records of STUDENT : %d \n", i+1);
         printf(" Id is: %d \n", record[i].id);
         printf(" Name is: %s \n", record[i].name);
         printf(" Percentage is: %f\n\n",record[i].percentage);
     }
     return 0;
}

I am having a difficult time debugging this code. For some reason the lines that I marked(-->) breaks the code. Right now this line and the one underneath are commented out because the code doesn't execute with them. I have a feeling it has to do something with the null terminator.

bluestar99
  • 11
  • 6

1 Answers1

6

struct student record[2]; means that the array has 2 entries. The valid indices are 0 and 1.

Trying to access record[2] causes undefined behaviour.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365