c中未处理的异常

时间:2014-12-13 11:57:44

标签: 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;
    }
}

1 个答案:

答案 0 :(得分:0)

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

  • 未定义使用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;
}}