What does a Variable-Length Array mean?
Variable-Length Array term is used for runtime sized or variable sized arrays. The size of such arrays is defined at run-time.
Variably modified types must be declared at either block scope or function prototype scope and include:
- variable-length arrays and
- pointers to variable-length arrays.
Variable-Length Array is a feature where we can allocate an auto array (on stack) of variable size. It can be used in a typedef statement.
Example to show Variable-Length Array
Below program compiles and runs fine in C language:
#include <stdio.h> void fun(int n) { // Creating a Variable-Length Array // based on the size passed as Input to this function int arr[n]; // ...... } int main() { // Passing a size for creation of // Variable Length Array at runtime fun(6); }
GFact | Why doesn’t C++ have Variable Length Arrays library?
While studying DSA, we all have encountered Arrays and their fixed-size property. But have you ever thought about Why C language allows us the Variable-Length Array but C++ doesn’t? And what should we do in C++ if we want a Variable-Length Array?
Well, we will talk about the above in detail in the following post but first, we should know about the term Variable-length Array.