0

So i created a structure called product and i wanted to sort an array with that structure type based in a component of that structure called price, and to do that i copied a merged sort algorithm.

I altered it a bit to sort the array the way i wanted and it works if i compile the file with normal gcc. The problem is that i can only compile the file using this command "gcc -Wall -Wextra -Werror -ansi -pedantic".

Structure:

typedef struct product 
{
   int ident;
   char desc[64]; /* string that describes a product eg. "bread" */
   int price;  /* price of the product*/
   int weight; /* weight of the product eg. 2kg */
   int quant; /* quantity of the product in stock */
   int state_prod;
}product;

Merged sort algorithm:

void merge(product arr[], int l, int m, int r)
{ 
    int i, j, k; 
    int n1 = m - l + 1; 
    int n2 =  r - m; 

    product L[n1];  // line of the error
    product R[n2];  // line of the error

    for (i = 0; i < n1; i++) 
        L[i] = arr[l + i]; 
    for (j = 0; j < n2; j++) 
        R[j] = arr[m + 1+ j]; 

    i = 0; 
    j = 0;
    k = l; 
    while (i < n1 && j < n2) 
    { 
        if (L[i].price < R[j].price) 
        { 
            arr[k] = L[i]; 
            i++; 
        } 
        else if (L[i].price == R[j].price)
        {
           if (L[i].ident < R[j].ident)
           {
              arr[k] = L[i]; 
            i++;
           }
           else
           {
              arr[k] = R[j]; 
            j++;
           }
        }
        else
        { 
            arr[k] = R[j]; 
            j++; 
        } 
        k++; 
    } 

    while (i < n1) 
    { 
        arr[k] = L[i]; 
        i++; 
        k++; 
    } 

    while (j < n2) 
    { 
        arr[k] = R[j]; 
        j++; 
        k++; 
    } 
} 

void mergeSort(product arr[], int l, int r)
{ 
    if (l < r) 
    { 
        int m = l+(r-l)/2; 

        mergeSort(arr, l, m); 
        mergeSort(arr, m+1, r); 

        merge(arr, l, m, r); 
    } 
}

When im compiling im getting this error:

In function ‘merge’:
error: ISO C90 forbids variable length array ‘L’ [-Werror=vla]
     product L[n1];
     ^~~~~~~
error: ISO C90 forbids variable length array ‘R’ [-Werror=vla]
     product R[n2];
     ^~~~~~~

Seriously any help would be appreciated.

M.M
  • 138,810
  • 21
  • 208
  • 365
Martimc.
  • 53
  • 1
  • 6
  • The error message is clear — what you're doing uses a VLA and those were added to C99 and are not part of C90. Therefore, you can't do that. You'll have to fall back on dynamic array allocation — `malloc()` and `free()`. – Jonathan Leffler Mar 30 '20 at 00:56
  • 2
    **Why** can you only use `... -ansi`? – Keith Thompson Mar 30 '20 at 01:09
  • 2
    Or, **Why** can't you use standard C (from this century)? If you're taking a class which insists on teaching C89, you need to find a different class. – rici Mar 30 '20 at 01:29

2 Answers2

1
product L[n1];  // line of the error

This line defines a variable length array. That is, an array whose size is not known at compile time. This feature was not added to the C language until the C99 revision, but GCC's -ansi flag specifies to use the rules from the older C90 revision of the language.

In older revisions of the C language, your options are to use non-standard platform features like alloca (don't do this unless you really know what you're doing) or to use malloc/free to do dynamic memory allocation:

product* L = malloc(n1 * sizeof(product));
// ...
free(L);
Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
  • Or simply declare an array of the maximum size you'll need. If one knows what the maximum size is, and knows that there would always be enough stack space to accommodate whatever array size one would be using, one could in most cases simply declare an array of the maximum size one might need. The only scenario where that would fail but a VLA would work would is if a function is sometimes used in circumstances where the array would be large but a lot of stack is available, and sometimes in circumstances where little stack is available but the array would be small. Pretty rare, really. – supercat Mar 31 '20 at 20:15
0

Use proper dynamic array instead. Like:

product *L = malloc(n1 * sizeof(product));

Don’t forget to free it after finishing using (at function end):

free(L);

numzero
  • 2,009
  • 6
  • 6