0

I'm tring to code a C code that sorts the args given from the smallest to the largest, but I keep getting this error :

warning: assignment to ‘int’ from ‘int *’ makes integer from pointer without a cast [-Wint-conversion] 80 | array[x] = av[i];

I tried to change "int array[x];" to "int *array[x];" but I get another error because of this line -> gnome_sort(array, size);

Can someone help me with this? Thx

void gnome_sort(int *array, int size)
{
    int tmp; 
    for (int i = 1; i < size;) {
        if (array[i-1] <= array[i])
            ++i;
        else {
            tmp = array[i];
            array[i] = array[i-1];
            array[i-1] = tmp;
            --i;
            if (i == 0)
                i = 1;
        }
    }
}
 
int main(int ac, char **av)
{
    int i;
    int x = 0;
    int array[ac];
    x = 0;
    for (i = 1; av[i] != NULL; i++) {
        array[x] = atoi(av[i]);
        x++;
    }
    size_t size = sizeof(array)/sizeof(array[0]);
    for (i = 0; i < size; i++)
        gnome_sort(array, size);
    for (i = 0; i < size; ++i)
        printf("%d ", array[i]);
    return 0;
}
8
  • 2
    main(int ac, int **av) is wrong among other things. Commented Dec 25, 2021 at 15:29
  • @Fixx This for loop for (x = 0; av[i] != NULL; x++); is an infinite loop.:) Commented Dec 25, 2021 at 15:30
  • I fixed it to main(int ac, char **av) but it doesnt change much I keep getting the errors Commented Dec 25, 2021 at 15:31
  • 1
    array[x] = av[i]; this line is wrong. array is an array of integers while av is an array of pointers to char. Commented Dec 25, 2021 at 15:44
  • 1
    If you're trying to get the numeric value of each argument, use array[x] = atoi(av[i]); Commented Dec 25, 2021 at 15:45

1 Answer 1

0

i have a suggestion not a solution, tray to pass the array to the function rather than using a void function and return it after modification.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.