0

my code is compiled well, and executed. But I get this error after I enter the sortType value: "Unhandled exception at 0x52a56af2 (msvcr90d.dll) in ALINUR_CAGLAYAN_LAB6.exe: 0xC0000005: Access violation writing location 0x00000000."

Here's my code:

#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;
    }
}
splrs
  • 2,424
  • 2
  • 19
  • 29
  • `sorting(*numbers, &length, &sortType);` -->> `sorting(numbers, &length, &sortType);` BTW: the compiler should *at least* give a warning. – wildplasser Dec 13 '14 at 12:00
  • And it's a good practice _not_ to use pointers where they are not needed. Like in your sorting function with `int *length`. You will get `Segmentation fault` on running it, because you are trying to use your `length` _pointer_ instead of a _value_. Same with `sortType`. You should read more about pointers and how to use it. – Ternvein Dec 13 '14 at 12:06
  • @wildplasser I still get the same error. Ternvein; It's a HW, they want us to use them. I need to work more for it, I know that. I'm still a novice. – Alinur Çağlayan Dec 13 '14 at 12:08

1 Answers1

0

There are multiple errors in your code.

  • The header file for using malloc is not defines.
  • scanf needs to access addresses and not variables.
  • The array name decays to a pointer so using * for an array is double dereferencing
  • To access value in pointer you have to use *, the dereferencing pointer.

I have attached the corrected code.

#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;
}}
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • 1
    I think it's much more clear just not to use pointers in sorting function at all, except for `int *numbers`. They're never changed inside the function anyway. – Ternvein Dec 13 '14 at 12:13
  • @Ternvein Yes, I don't know what on earth the OP wants to acheive by using pointers to such a bad extent – Bhargav Rao Dec 13 '14 at 12:14
  • 1
    [Arrays are not pointers](http://stackoverflow.com/questions/1641957/is-array-name-a-pointer-in-c). – Quentin Dec 13 '14 at 14:02