Search code examples
caccess-violation

Access violation 0xc00005 in C


I have been writing a program that accepts student information through an array of structure and then performs various operations on it.

Whenever I try to perform any of the two operations i.e 1 and 2, I get an access violation.

Here is the code.

#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);
    }
}

Solution

  • David has already pointed out the problem with strings. The second problem is here:

    scanf("%d",choice);
    

    You forgot the &:

    scanf("%d", &choice);
    

    Tip: if you used a debugger, you would have immediately found the line and I'm sure you would have discovered the typo yourself.