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.