0

For some reason that I'm not aware of, array from main() doesn't get to be modified when it should. Isn't "a" passed by reference? Can someone please guide me a little bit?

Here is the code:

#include "stdio.h"

void sort(int list[])
{
    //int list[] = { 4, 3, 2, 1, 10 };
    int n = sizeof(list) / sizeof(int);
    int min = 0;
    int temp = 0;
    for (int i = 0; i < n; i++)
    {
        min = i;
        //profiler.countOperation("compSort", n, 1);
        for (int j = i + 1; j < n; j++)
        {
            if (list[j] < list[min])
            {
                min = j;
                //profiler.countOperation("compSort", n, 1);
                    }
        }
        if (min != i)
        {
            //profiler.countOperation("compSort", n, 1);
            temp = list[min];
            list[min] = list[i];
            list[i] = temp;
        }
    }
}

int main()
{
    int a[5] = {4, 3, 2, 1, 10};
    sort(a);
    printf("%d\n", a[0]);
    for (int i = 0; i < 5; i++)
    {
        printf("%d", a[i]);
    }
    return 0;
    /*int arr[MAX_SIZE];
    for(int t = 0; t < 1; t++)
    {
        for (int n = 100; n < 3000; n = n + 300)
        {
            FillRandomArray(arr, n);
            sort();
            printf("done %d \n", n);

            if (!IsSorted(arr, n)
            {
                printf("error sort \n");
            }
        }
    }
    profiler.addSeries("TotalSort", "cmpSel", "atribSel");
    profiler.createGroup("SortMediu", "TotalSort", "cmpSel", "atribSel");
    profiler.showReport();*/
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
udarH3
  • 199
  • 2
  • 5
  • 16

2 Answers2

4

The problem is with

 int n = sizeof(list) / sizeof(int);

arrays, once passed as function arguments decays to the pointer to the first element. They don't have array properties anymore. So, the list here is adjusted as a pointer to int.

Quoting C11, chapter §6.7.6.3

A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation.

Solution: You need to calculate the size at the caller (before the array decay happens) and pass that as a different argument to the called function.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

Array passed to function in that way decays to pointer so

int n = sizeof(list) / sizeof(int);

Result in sizeof(int *)/sizeof(int)

You can use VLAs to sole the problem

#include "stdio.h"

void sort(size_t n, int list[n])
{
    size_t min = 0;
    int temp = 0;

    for (size_t i = 0; i < n; i++)
    {
        min = i;
        //profiler.countOperation("compSort", n, 1);
        for (size_t j = i + 1; j < n; j++)
        {
            if (list[j] < list[min])
            {
                min = j;
                //profiler.countOperation("compSort", n, 1);
            }
        }
        if (min != i)
        {
            //profiler.countOperation("compSort", n, 1);
            temp = list[min];
            list[min] = list[i];
            list[i] = temp;
        }
    }
}

int main(void)
{
    int a[5] = { 4, 3, 2, 1, 10 };
    sort(sizeof(a) / sizeof(a[0]), a);
    printf("%d\n", a[0]);
    for (int i = 0; i < 5; i++)
    {
        printf("%d", a[i]);
    }
    printf("\n");

    return 0;
}
LPs
  • 16,045
  • 8
  • 30
  • 61