Link to home
Start Free TrialLog in
Avatar of cookiejar
cookiejarFlag for United States of America

asked on

Exception thrown at 0x53730ED0 (ucrtbased.dll) in 0xC0000005: Access violation reading location 0xCDA9C0F0

I am loading data into a struct array.  I am using while loop to prompt for first and last name.  The user should be able to exit the loop by enter a '0' in either First Name or Last Name.  I getting and exception error.  I am a newbie, so forgive me for not choosing the most efficient approach.  Welcome any recommendations
Please tell me what I am doing wrong.  I am encountering this error:
Exception thrown at 0x53730ED0 (ucrtbased.dll) in grader.exe: 0xC0000005: Access violation reading location 0xCDA9C0F0

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STUDENTS 30

struct student
{
  char fname[15];
  char lname[10];
  float percentGrade;
  char letterGrade;
};
typedef struct student STUDENT;

void enterStudents(STUDENT [], int *);

main()

{

    STUDENT students[MAX_STUDENTS];
    int studentCount = 0;
      
      enterStudents(students, &studentCount);

      
    return(0);
}

void enterStudents(STUDENT students[], int * ptr_studentCount)
  {
     

    /*  Prompt for initial input  */
    printf("\n Enter First Name or (0) to exit :");
    scanf(" %14[^\n]", students[*ptr_studentCount].fname); //adding the space and deleting the s
    fflush(stdin);
                                                           /*  Prompt for initial input  */
    printf("\n Enter Last Name or (0) to exit :");
    scanf(" %9[^\n]", students[*ptr_studentCount].lname); //adding the space and deleting the s
    fflush(stdin);
   
   
    while (strcmp(students[*ptr_studentCount].fname,"0") || strcmp(students[*ptr_studentCount].lname, "0") )
    {
           
      // Prompt for first name
      printf("\n Enter Student First Name or (0) to exit :");
      scanf(" %14[^\n]", students[*ptr_studentCount].fname);
      fflush(stdin);

      /*  Prompt for last name  */
      printf("\n Enter Student Last Name or (0) to exit :");
      scanf(" %9[^\n]", students[*ptr_studentCount].lname);
      fflush(stdin);

      *ptr_studentCount++;
     
    }
    system("pause");
 }
 
 Exception thrown at 0x53730ED0 (ucrtbased.dll) in grader.exe: 0xC0000005: Access violation reading location 0xCDA9C0F0.
Avatar of sarabande
sarabande
Flag of Luxembourg image

*ptr_studentCount++;
there is no code which would prevent the statement to increase the index beyond the maximum count of the passed array.

Sara
you may not pass the current index to the function by pointer. instead pass the maximum count by value, have a local counter for the current index in the function and return the last index when as return value to the caller.

also the student array should be initialized. do it simply by

STUDENT students[MAX_STUDENTS] = { 0 };

Open in new window


Sara
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial