开发者

Declaring an array with a non-constant size variable [duplicate]

开发者 https://www.devze.com 2023-01-17 18:24 出处:网络
This question already has answers here: 开发者_运维技巧 Which compiler should I trust? (4 answers)
This question already has answers here: 开发者_运维技巧 Which compiler should I trust? (4 answers) Closed 4 years ago.

I'm studying for my test in C and I'm reading in a C summary I downloaded from some site. It is written that it is not allowed to write arr[i] where i is a variable. The only way to do it is with malloc.

However, I wrote the following code and it compiles without warnings and without error on 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 behind array declarations? thank you!


C99 allows variable length arrays to be created on the stack. Your compiler may support this feature. This features is not available in C89.

What the summary told you was true, from a certain point of view. :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消