I am new to C programming, I am trying to run a code snippet that create a dynamic array of struct object and then uses scanf to save values. When I run the code in code blocks it works just fine but throws memory access exceptions in Visual studio 2019
I am attaching code snippet and error trace.
struct Student
{
int rollNumber;
char studentName[10];
float percentage;
};
int main(void)
{
int counter;
struct Student studentRecord[5];
printf("Enter Records of 5 students");
for (counter = 0; counter < 5; counter++)
{
printf("\nEnter Roll Number:");
scanf_s("%d", &studentRecord[counter].rollNumber);
printf("\nEnter Name:");
scanf_s("%s", &studentRecord[counter].studentName);
printf("\nEnter percentage:");
scanf_s("%f", &studentRecord[counter].percentage);
}
printf("\nStudent Information List:");
for (counter = 0; counter < 5; counter++)
{
printf("\nRoll Number:%d\t Name:%s\t Percentage :%f\n",
studentRecord[counter].rollNumber, studentRecord[counter].studentName, studentRecord[counter].percentage);
}
return 0;
}
Error:
Exception thrown at 0x7C9AEF8C (ucrtbased.dll) in linkedList.exe: 0xC0000005: Access violation writing location 0x00500000.