Index '5' is outside the valid index range '0' - '4' for buffer 'grade' that may have been assigned to C language C6201 stack.

Asked 2 weeks ago, Updated 2 weeks ago, 2 views

#include <stdio.h>
#define size 5

int main(void)
{
    int grade[size]; {};
     grade[size] = 0;

    int i, min, max, sel; {
        for (i = 0; i < size; i++)
        {
            printf ("Enter your grade:");
            scanf_s("%d", &grade[i]);
        }
        printf("Select menu: 1. Minimum 2. Maximum\n");
        scanf_s("%d", &sel);

        min = grade[0];
        max = grade[0];

        for (i = 1; i < size; i++)
        {
            if (sel == 1)
            {
                if (min > grade[i])
                    min = grade[i];
            }
            else
            {
                if (max < grade[i])
                    max = grade[i];
            }
        }
        if (sel == 1)
            printf ("Minimum value is %d".\n", grade[i]);
        else
            printf ("Maximum value is %d".\n", grade[i]);
        return 0;
    }
}

There is an error, but I don't know how to handle it.

Severity code descriptionProject file line display error status
A buffer overrun occurred while writing to warning C6386 'grade'.    
Project19 C:\Users\Lee Min-jae\source\repos\Project19\Project19\source.c16  

Severity Code Description Project File Line Display Error (Suppression) Status
Error C4789 Buffer 'grade' (size: 20 bytes) is overrun. 4 bytes are written from offset 20.  
Project19 C:\Users\Lee Min-jae\source\repos\Project19\Project19\source.c16  

Severity Code Description Project File Line Display Error (Suppression) Status
Warning Index '5' is outside the valid index range '0' - '4' for buffer 'grade' that may have been assigned to stack C6201.  
Project19 C:\Users\Lee Min-jae\source\repos\Project19\Project19\source.c16  

c

2022-09-20 10:21

1 Answers

    int grade[size]; {};
     grade[size] = 0;

Change the top two lines as below.

int grade[size];


2022-09-20 10:21

If you have any answers or tips


© 2022 pinfo. All rights reserved.