c - c 中未处理的异常

标签 c

我的代码编译良好并执行。但在输入 sortType 值后出现此错误:“ALINUR_CAGLAYAN_LAB6.exe 中 0x52a56af2 (msvcr90d.dll) 处出现未处理的异常:0xC0000005:访问冲突写入位置 0x00000000。”

这是我的代码:

#include <stdio.h>

int sorting(int *liverpool8, int *besiktas0, int *hahaha);

void main()
{
    int *numbers;
    int length;
    int sortType=0;
    int i;


    printf("Enter the length of array:");
    scanf("%d",&length);

    numbers = (int*)malloc(length*sizeof(int));

    for (i = 0; i < length; ++i)
    {
        printf("%d. element: ", i+1);
        scanf("%d", &numbers[i]);
    }
    printf("\nPlease select one of the following functions:\n1)Ascending order\n0)Descending order");
    scanf("%d", sortType);
    sorting(*numbers, &length, &sortType);
    printf("The numbers arranged in the order as you entered are given below\n");
    for (i = 0; i < length; ++i)
    {
        printf("%d\n", numbers[i]);
    }
    system("pause");
}

int sorting(int *numbers, int *length, int *sortType)
{   
    int j, i, a;
    if(sortType == 1)
    {
        for (i = 0; i < length; ++i)
        {
            for (j = i + 1; j < length; ++j)
            {
                if (numbers[i] > numbers[j])
                {
                    a =  numbers[i];
                    numbers[i] = numbers[j];
                    numbers[j] = a;
                }
            }
        }
    }
    else if(sortType == 0)
    {
        for (i = 0; i < length; ++i)
        {
            for (j = i + 1; j < length; ++j)
            {
                if (numbers[i] < numbers[j])
                {
                    a = numbers[i];
                    numbers[i] = numbers[j];
                    numbers[j] = a;
                }
            }
        }
        return *numbers;
    }
}

最佳答案

您的代码中存在多个错误。

  • 未定义使用malloc的头文件。
  • scanf 需要访问地址而不是变量。
  • 数组名称会衰减为指针,因此对数组使用 * 是双重解引用
  • 要访问指针中的值,您必须使用 *(解引用指针)。

我已附上更正后的代码。

#include <stdio.h>
#include <stdlib.h>
int sorting(int *liverpool8, int *besiktas0, int *hahaha);

void main()
{
int *numbers;
int length;
int sortType=0;
int i;


printf("Enter the length of array:");
scanf("%d",&length);

numbers = (int*)malloc(length*sizeof(int));

for (i = 0; i < length; ++i)
{
    printf("%d. element: ", i+1);
    scanf("%d", &numbers[i]);
}
printf("\nPlease select one of the following functions:\n1)Ascending order\n0)Descending order");
scanf("%d", &sortType);
sorting(numbers, &length, &sortType);
printf("The numbers arranged in the order as you entered are given below\n");
for (i = 0; i < length; ++i)
{
    printf("%d\n", numbers[i]);
}
system("pause");
}
int sorting(int *numbers, int *length, int *sortType)
{   
int j, i, a;
if(*sortType == 1)
{
for (i = 0; i < *length; ++i)
{
    for (j = i + 1; j < *length; ++j)
    {
        if (numbers[i] > numbers[j])
        {
            a =  numbers[i];
            numbers[i] = numbers[j];
            numbers[j] = a;
        }
    }
}
}
else if(sortType == 0)
{
for (i = 0; i < *length; ++i)
{
    for (j = i + 1; j < *length; ++j)
    {
        if (numbers[i] < numbers[j])
        {
            a = numbers[i];
            numbers[i] = numbers[j];
            numbers[j] = a;
        }
    }
}
return *numbers;
}}

关于c - c 中未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27458523/

相关文章:

C、在open()等函数中使用F命令

c - OpenCL:从 'int *' 到 '__generic int *__generic *' 的转换

c - 如何为我的自定义错误编号

C fopen() 错误 : incompatible types when assigning to type ‘FILE’ from type ‘struct FILE *’

c - 如何删除空格并检查字符串是否是回文?

c - 为什么我的代码无法运行?指针?

c++ - 用 C 或 C++ 编写图形 Z80 仿真器

c - 如何使用 fork() 函数在 C 中守护进程。 - 套接字编程

c - 关于大学项目的问题

使用调试寄存器断点和向量化异常处理捕获堆栈跟踪