Я писал программу, которая принимает информацию о студентах через массив структуры, а затем выполняет различные операции над ним.Нарушение прав доступа 0xc00005 в C
Всякий раз, когда я пытаюсь выполнить любую из двух операций i.e 1 и 2, я получаю нарушение доступа.
Вот код.
#include <stdio.h>
void linear();
void bubble();
void binary();
void insertion();
int i, j, temp;
struct kaksha{
int RegNo;
char Name[50];
char Branch[50];
float GradePoint;
};
int main()
{
int size, choice;
struct kaksha *ptr;
ptr = (struct kaksha *)malloc(sizeof(struct kaksha)); //Allocate memory to the structure.
struct kaksha jwak[50];
printf("Enter the number of students.\n");
scanf("%d", &size);
printf("Enter the respective details.\n");
printf(" \n");
for (i = 1 ; i < size + 1; i++)
{
printf("Enter registration number for student number %d.\n", i);
scanf("%d", &jwak[i].RegNo);
printf("Enter CGPA for student number %d.\n", i);
scanf("%f", &jwak[i].GradePoint);
printf("Enter name for student number %d.\n", i);
scanf("%s", &jwak[i].Name);
//gets(jwak[i].Name);
printf("Enter branch of student number %d.\n", i);
scanf("%s", &jwak[i].Branch);
//gets(jwak[i].Branch);
}
printf("Select the operation to be formed.\n");
printf("1. Call linear search function to display data of student with a particular registration number.\n");
printf("2. Call bubble sort function to arrange data of students according to registration number.\n");
printf("3. Apply binary search on the output of option 2 to display data of a student with a particular registration number.\n");
printf("4. Use and modify Insertion sort logic to arrange data of students in descending order of CGPA.\n");
scanf("%d",choice);
switch(choice)
{
case 1:
linear(jwak,size);
case 2:
bubble(jwak,size);
case 3:
binary(jwak,size);
case 4:
insertion(jwak,size);
}
}
void linear(struct kaksha jwak[],int size)
{
int query;
printf("Enter the register number of the student you wish to query.\n");
scanf("%d", &query);
for (i = 0 ; i < size ; i++)
{
if (jwak[i].RegNo == query)
{
printf("Name of the student is %s.\n",jwak[i].Name);
printf("Branch of the student is %d.\n",jwak[i].Branch);
printf("Grade point of the student is %f.\n",jwak[i].GradePoint);
}
else
{
printf("Corresponding entry does not exist.\n");
}
}
}
void bubble(struct kaksha jwak[],int size)
{
for (i = 0 ; i < size ; i++)
{
for (j = 0 ; j < (size - i - 1) ; j++)
{
if (jwak[j].RegNo > jwak[j + 1].RegNo)
{
temp = jwak[j].RegNo;
jwak[j].RegNo = jwak[j + 1].RegNo;
jwak[j + 1].RegNo = temp;
}
}
}
printf("Data arranged according to increasing order of registration number.\n");
for (i = 0 ; i < size ; i++)
{
printf("Registration number of the student is %d.\n", jwak[i].RegNo);
printf("Name of the student is %s.\n", jwak[i].Name);
printf("Branch of the student is %d.\n", jwak[i].Branch);
printf("Grade point of the student is %f.\n", jwak[i].GradePoint);
}
}
сделать ваш код доступным для чтения, так что легче сказать, в чем проблема. Используйте отладчик, чтобы узнать, где эта проблема. –
Не отбрасывайте возврат malloc в C. Кроме того, включите, чтобы соответствующий прототип присутствовал для malloc. –
Пожалуйста, не просто отправляйте свой вопрос, когда вы не получили ответ в первый раз. – fuz