Well you can use SIZE = atoi( argv[1] );
where atoi() is declared in <cstdlib>
However, you shouldn't set the size of a standard array with anything that isn't known at compile time (though some compilers permit it). You should use either a dynamic array, created with new, or let the compiler do the memory management and use a vector<int>.
#include <iostream>
#include <cstdlib>
#include <vector>
usingnamespace std;
int main(int argc, char *argv[])
{
if (argc < 2)
{
cout << "Missing size parameter\n";
return 1;
}
int size = atoi(argv[1]);
if (size <= 0)
{
cout << "size " << argv[1] << " must be greater than zero\n";
return 1;
}
// C++ recommended way - use vector
vector<int> array1(size);
// Possible alternative, but not recommended - use new[] and delete []
int * array2 = newint[size];
/*
Code which uses the array
goes here
*/
// Before exit, de-allocate array2
delete [] array2;
return 0;
}
is not possible in standard C++, because len is not a compile-time constant. It may be allowed by your compiler, but could fail if a different compiler is used. It is best not to rely on such quirks, especially when learning. Use a vector, or maybe new [] and delete[].