• C

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.
cookiejarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Sara
0
sarabandeCommented:
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
0
sarabandeCommented:
if you apply my suggestions the call would change to

int studentCount = enterStudents(students, MAX_STUDENTS);

Open in new window


and in the enterStudents function you would use a local int variable student_count instead of *ptr_studentCount.

Sara
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.