C 中的访问冲突 0xc00005

Access violation 0xc00005 in C

我一直在写一个程序,通过一个结构数组接受学生信息,然后对其进行各种操作。

每当我尝试执行这两个操作(即 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);
    }
}

我看到几个重要的问题

  1. 您永远不会检查 size 是否小于 50,这是数组的大小。
  2. 您在 i == 1 处开始循环并在 i < size + 1 处结束循环。在 中,数组的索引从 0size - 1 所以它应该是

    for (int i = 0 ; i < size ; ++i)
    
  3. 使用 "%s" 说明符调用 scanf() 时,不应传递数组的地址,数组会自动转换为指向其第一个元素的指针&arrayarray是一样的,但是&array + 1array + 1不会一样。 ()

  4. 在你的 switch 语句中没有中断,所以所有的情况都失败了,这意味着如果用户选择 1 例如,1 , 2, 3, 4 ...等都会被执行

  5. 如前所述 不包括 stdlib.h malloc() 的原型是错误的,这将导致未定义行为。编译器应该告诉你“警告:内置函数的隐式声明malloc()你可能会忽略它,但你不应该。

  6. 您总是忽略 scanf() 的 return 值,这也可能导致未定义的行为。

当您为字符串调用 scanf 时,您 不会 传递地址。

scanf("%s",&jwak[i].Name);

你这样做了好几次。您确实需要为整数、浮点数等执行此操作,但请记住,在 C 中,您使用的字节数组 already (有效地)是一个指针。所以:

scanf("%s",jwak[i].Name);

这几乎可以肯定是访问冲突的根源。

此外,正如我在评论中提到的,在 C 中使用 malloc 时,不要强制转换 return 值。此外,包括 <stdlib.h> 以便您拥有 malloc.

的正确原型

David has already pointed out the 。第二个问题在这里:

scanf("%d",choice);

您忘记了 &:

scanf("%d", &choice);

提示:如果您使用调试器,您会立即找到该行,我相信您会自己发现拼写错误。