Understanding Array Initialization with Variable vs. Numeric Literals
Problem:
In C , attempting to initialize an array with a variable length, such as int n = 10; double tenorData[n] = {1, 2, 3, ...}, results in a compilation error. However, initializing with a fixed length, like double tenorData[10], succeeds. Why does this occur?
Answer:
In C , variable-sized arrays are not permissible. While extensions in certain compilers (e.g., G ) allow them, they remain illegal according to the standard. To create arrays with variable lengths in C , you can either:
If you still require an array, consider using a constant value instead of a variable:
The above is the detailed content of Why Can't I Initialize a C Array with a Variable Length?. For more information, please follow other related articles on the PHP Chinese website!