Whenever I need to create an array with a number of elements not known until execution time I do this.
int n, i;
printf("Number of elements: ");
scanf("%d", &n);
int myArray[n];
for(i = 0; i < n; i++)
myArray[i] = 0;
However I've been told by 3 people with a PhD in Computer Science not to do it, because "it's not guaranteed to work on every compiler", and that the number of the elements in an array must be known at compile-time. So they do that like this.
int myArray[1000];
int n, i;
printf("Number of elements: ");
scanf("%d, &n);
//we must stop at the n element
for(i = 0; i < n; i++)
myArray[i] = 0;
Which one should I use? When it's not guaranteed to work? Is it just a memory waste or a need to maintain legacy?