Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable Sized Arrays in C

Tags:

arrays

c

I guess my question is whether the following is valid C

int main(void) {
  int r = 3;
  int k[r];
  return 0;
}

If so, would some one care to explain why it does not work in Microsoft's C compiler, but in GCC, and when it was added to the C standard.

Thank you

like image 959
adk Avatar asked Jun 28 '09 02:06

adk


People also ask

Are variable length arrays bad C?

Safe and useful variable length arrays It's convenient and useful because it makes some expressions simpler. It's safe because there's no arbitrary stack allocation. Pointers to arrays are a rare sight in C code, whether variable length or not.

What is variable sized?

Variable-size data is data whose size is not known at compile time or whose size can change at run time.

Does C support VLA?

Default. C90 and Standard C++ do not support variable length arrays by default. Select the option --vla to enable support for variable length arrays in C90 or Standard C++. Variable length arrays are supported in Standard C.


2 Answers

It is in C99. MSVC only supports C89.

like image 60
rlbond Avatar answered Oct 27 '22 18:10

rlbond


The C99 standard added variable-length arrays, but other vendors such as GCC added them much earlier.

like image 35
rpetrich Avatar answered Oct 27 '22 20:10

rpetrich