When i run the following code i got an error;
Program received signal SIGSEGV, Segmentation fault. 0x00007ffff7a74402 in __GI__IO_vfscanf () from /lib64/libc.so.6 Missing separate debuginfos, use: debuginfo-install glibc-2.17-78.el7.x86_64
i run my code using gcc warnings enable(-Wall) and debug it step by step. The error is in the line for getting multi-dimensional array elements. When i reach at the step for grades[1][2], it says cannot access memory at address 0x00007ffff7a74402. What is the wrong in the code?
#include <stdio.h> //_________Prototypes________// int calcHighest(int grades[][4],int); int calcLowest(int grades[][4],int); int calcAverage(int grades[][4],int); int main (void){ int funcSelection,nbOfStudent; int grades[nbOfStudent][4]; int i,j; scanf("%d",&funcSelection); //check is one 1,2,3 and a number if(funcSelection <= 0 || funcSelection > 3){ printf("Invalid Input.Please enter 1,2 or 3\n"); return 1; } scanf("%d",&nbOfStudent); //check if it is positive and a number if(nbOfStudent <= 0){ printf("Please enter a non-negative number\n"); return 1; } for(i = 0;i < nbOfStudent;i++){ for(j =0;j<4;j++){ scanf("%d",&grades[i][j]); } } for(i = 0;i < nbOfStudent;i++){ for(j =0;j<4;j++){ scanf("%d",&grades[i][j]); } } if(funcSelection == 1){ printf("%d\n",calcLowest(grades,nbOfStudent)); }else if(funcSelection == 2){ printf("%d\n",calcHighest(grades,nbOfStudent)); }else{ printf("%d\n",calcAverage(grades,nbOfStudent)); } return 0; }
int grades[nbOfStudent][4];
as yourself what the value ofnbOfStudent
is when that declaration is encountered. Hint:scanf
-ing that variable later doesn't change what came before.