0

i am trying to do this code-wars challenge (do not spoiler pls) and i am having problems initializing my array. I simply want to have an array of the size of variable n where every number is a 0; still when i try to do so my compiler tells me that it is not possible. If i hard-code the desired number there is not a problem for the compiler. Why is that and how can i solve this?



#include <stdlib.h>

int queue_time(const int *customers, int customers_length, int n)
{ int totalt = 0;
  int timelist[n] = {0}; // <- this is the array i want to initialize
  int i = 0;
  int c = 0;
  int x = 0;
  int lowestval = 0;
 
while (c < n)
{
if (timelist[i] = 0)
{ timelist[i] = customers[c];
  c++;
} 
  while (timelist[i] != 0 && i < n)
    i++;
  
if(i == n)
{
  i = 0;
  lowestval = timelist[i];
while (i < n)
{
  if (lowestval > timelist[i])
    lowestval = timelist[i];
}
}
    
}
return totalt;
}

int main(void)
{
    queue_time((int []){1, 2, 3, 4}, 4, 1);
}

i tried to initialize an integer array of a size depending on the variable n given from the main function. The program won't let me, i already tried initializing the array with malloc instead but it still tells me that it is not possible to initialize the array with the variable n.

Felix
  • 1
  • 2
  • 2
    MSVC is a benighted compiler that literally still can't support last century's C standard despite the fact this century is well nigh 1/4 over already. – Andrew Henle Oct 26 '22 at 13:23
  • 2
    ... but even this century's latest version of the C language does not provide for variable-length arrays to be initialized in their declarations. Why your `malloc()` attempt was rejected is impossible to say without looking at that version of the code, but my crystal ball tells me that you kept type `int[n]` instead of switching to `int *`. – John Bollinger Oct 26 '22 at 13:28

0 Answers0