Search code examples
carraysdeclare

why a[n] is accepted in c during runtime?


why can we do this in c?

int n;
scanf("%d",&n);
int a[n];

I thought array is located memory during load time but seems like the above example works during runtime. Do I misunderstand any thing? can you guys help?

Thanks,


Solution

  • I thought array is *al*located memory during load time but seems like the above example works during run-time.

    Yes, ordinary arrays like <datatype> <Array_Name> [<size>] is allocated memory during load time it is there in C89 and also existed in C99.

    But in the code snippet int a[n]; is a Variable Length Array or VLA for short.VLA's in C99 are defined just like any other array, except that the length doesn’t need to be a compile-time constant.

    A decent article on the need of VLAs can be found here :http://www.ddj.com/cpp/184401444 :)