Monday, July 13, 2015

Variable Length Array in C and C++


Traditionally C did not support variable length arrays. This is true for both K&R and C89 compilers.
The C99 standard introduced support for the variable length arrays.

The interesting part of this is that C++ never supported the variable length arrays.  If you needed the variable length arrays in C++ vectors in the standard library was your only option.


#include <stdio.h>
void foo(int n)
{
int arr[n];
printf("sizeof %d \n", sizeof(arr));
}

int main()
{
  foo(5);
}

The output of this program is 20

No comments:

Post a Comment