Declaring an array with a variable not constant

I am learning for my C test, and I read in the C summary which I downloaded from some site. It is written what to write arr[i]where iis a variable. The only way to do this is with malloc.
However, I wrote the following code, and it compiles without warnings and without errors in valgrind:

int index = 5;
int a4[index];

a4[0] = 1;
a4[1] = 2;

int index2;
scanf("%d",&index2);
int a5[index2];
a5[0] = 1;
a5[1] = 2;

So what is the truth in array declarations? thank!

+5
source share
1 answer

C99 allows you to create variable length arrays on the stack. Your compiler can support this feature. These features are not available on the C89.

What you were talking about was true, from a certain point of view. :-)

+14

All Articles