Home >Backend Development >C++ >Variable-Sized vs. Literal-Sized Arrays in C : Which Initialization Method Should You Use?
Array Initialization: Variable-Sized Arrays vs. Literal-Size Arrays
In C , arrays can be initialized with either a variable size or a numeric literal. However, there is a key distinction between these two methods of initialization that can lead to errors.
Variable-Sized Arrays
Declaring an array with a variable size, such as double tenorData[n], may not be legal in strict C . Variable length arrays are not part of the C standard, but some compilers, like G , may allow them as an extension. However, this can result in errors if the compiler is set to adhere to the C standard.
Numeric Literal Arrays
On the other hand, declaring an array with a numeric literal, such as double tenorData[10], is legal in C . It initializes the array with a fixed size of 10 elements.
Solution for Variable-Sized Arrays
If you require a dynamically sized array, there are several options available:
The above is the detailed content of Variable-Sized vs. Literal-Sized Arrays in C : Which Initialization Method Should You Use?. For more information, please follow other related articles on the PHP Chinese website!