As I know in C++, when we declare an array, we need a constant size as:
int arr[4];
or
#define SIZE 4;
int arr[SIZE].
This is likely due to the fact that the size of arrays must be known in compilation time.
However, I've realized that it is executable if get the size of an array from the keyboard (it means arrays size can be input in the run-time).
Here is an example code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Example program
#include <iostream>
int main()
{
unsigned n;
int arr[n];
std::cout<<"Specify the value of n:";std::cin>>n;
for(unsigned ii=0;ii<n;++ii){
arr[ii]=ii*10;
}
for(unsigned ii=0;ii<n;++ii){
std::cout<<arr[ii]<<"\t";
}
std::cout<<std::endl;
}
Is it a new update of C++ (even with compile C++98 it works) or I misunderstand somewhere?
Firstly, my question is if the declaration of arrays with changeable sizes is possible ? As I know (you can follow this tutorial http://www.cplusplus.com/doc/tutorial/arrays/) it says:
NOTE: The elements field within square brackets [], representing the number of elements in the array, must be a constant expression, since arrays are blocks of static memory whose size must be determined at compile time, before the program runs.
Secondly, the variable is undefined because it takes value from keyboard input.