Unable to write to memory after passing dynamic structure in C

Your current enterScores is accepting an array of pointers to struct testInfo, but what is actually passed is a pointer to single pointer to struct testInfo. This is treated as one-element array, and therefore accessing scores[1] and further is invalid.

What you want seems:

Declaration of the function:

void enterScores(struct testInfo scores[], int size);

(use the same scheme as printScores)

Call of the function:

                enterScores(testScores, size);

(remove &)

Definition of the function:

void enterScores(struct testInfo scores[], int size) {
    int i;
    char name[30];

    for (i = 0; i < size; i++) {
        printf("\nEnter student's name: ");
        fgets(name, 30, stdin);
        strcpy(scores[i].student, name);
        printf("\nEnter test score: ");
        scanf_s("%i", &scores[i].grade);
        while (scores[i].grade < 0 || scores[i].grade > 120) {
            printf("\nError in grade range (0-120), try again.");
            printf("\nEnter test score: ");
        }
        //determines letter grade from test Score
        if (scores[i].grade > 90) {
            scores[i].letterGrade = 'A';
        }
        if (scores[i].grade < 90 && scores[i].grade >= 80) {
            scores[i].letterGrade = 'B';
        }
        if (scores[i].grade < 80 && scores[i].grade >= 70) {
            scores[i].letterGrade = 'C';
        }
        if (scores[i].grade < 70 && scores[i].grade >= 60) {
            scores[i].letterGrade = 'D';
        }
        if (scores[i].grade < 60 && scores[i].grade >= 0) {
            scores[i].letterGrade = 'F';
        }
    }
}//end enterScores

(change the argumen and replace -> with .)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top