I can't understand why I'm receiving this error. I'm new to arrays so I can unders
tand that I might have made a mistake somewhere.
make -s
./main
Enter number of students: 1
For student 1,
Enter Surname: test
Enter Mark for MOD100: 1
Enter Mark for MOD101: 2
Enter Mark for MOD102: 3
Enter Mark for MOD103: 4
Enter Mark for MOD104: 5
Enter Mark for MOD105: 6
Student Name MOD100 MOD101 MOD102 MOD103MOD104 MOD105 Overall
signal: segmentation fault (core dumped)
As far as I can see my code is correct, the second part is a WIP.
This is my code:
`
#include <stdio.h>
int main() {
int numofstudents = 0;
int i;
int j;
int sum;
char names[numofstudents][1];
int modules[numofstudents][6];
printf("Enter number of students: ");
scanf("%i", &numofstudents);
for (i = 0; i < numofstudents; i)
{
printf("\nFor student %d,\n", i 1);
printf("Enter Surname: ");
scanf("%s", &names[i]);
printf("Enter Mark for MOD100: ");
scanf("%i", &modules[1][i]);
printf("Enter Mark for MOD101: ");
scanf("%i", &modules[2][i]);
printf("Enter Mark for MOD102: ");
scanf("%i", &modules[3][i]);
printf("Enter Mark for MOD103: ");
scanf("%i", &modules[4][i]);
printf("Enter Mark for MOD104: ");
scanf("%i", &modules[5][i]);
printf("Enter Mark for MOD105: ");
scanf("%i", &modules[6][i]);
}
printf("Student\t");
printf("Name\t");
printf("MOD100\t");
printf("MOD101\t");
printf("MOD102\t");
printf("MOD103\t");
printf("MOD104\t");
printf("MOD105\t");
printf("Overall\n");
for (i = 0; i < numofstudents; i)
{
printf("M\t", i 1);
printf("%4s\t", names[i][1]);
printf("%4i\%\t", modules[1][i]);
printf("%4i\%\t", modules[2][i]);
printf("%4i\%\t", modules[3][i]);
printf("%4i\%\t", modules[4][i]);
printf("%4i\%\t", modules[5][i]);
printf("%4i\%\t", modules[6][i]);
}
return 0;
}
`
I have tried entering the received values in different ways but this error appears every time.
CodePudding user response:
You arrays do not have any elements. When you try to access such array you always access it outside the bounds. It is an Undefined Befaviour.
You need to define arrays when you have scanned the user input.
int main() {
int numofstudents = 0;
int i;
int j;
int sum;
printf("Enter number of students: ");
if(scanf("%i", &numofstudents) != 1) {/* error handling*/}
char names[numofstudents][1];
int modules[numofstudents][6];
Also you need to index the array the way round ie : scanf("%i", &modules[i][1]);
Another problem is that indexes are from zero not one and you need to start from scanf("%i", &modules[i][0]);
and finish scanf("%i", &modules[i][5]);
CodePudding user response:
0_________ is correct that your indexes are wrong. I just wanted to point out that you are creating a Variable Length Array (VLA). int numofstudents = something;
followed by char names[numofstudents][1];
is creating a VLA because the value of numofstudents
will not be known at compile time. To avoid this, just make a constant sized array:
#define STUDENTS_MAX 100
int numofstudents = 0;
char names[STUDENTS_MAX][1];
scanf("%i", &numofstudents);
// Now, don't allow the user to enter anything over STUDENTS_MAX
if (numofstudents > STUDENTS_MAX) {
numofstudents = STUDENTS_MAX;
}
This will also solve your problem of writing out of bounds.
To avoid accidentally creating a VLA, you can use -Werror=vla
as build option in gcc or clang. Not sure if MSVC has an option like this.