Inputting needed size of array and keeping all elements = 0

After such code

1
2
input>>n>>k;
int array[n];


elements of array are no longer 0, I have tried:

1
2
3
 
input>>n>>k;
int array[n] = {0};
, but for some reason GNU GCC is giving me errors.


What's the proper way of inputing size of array from text file without messing up size of elements? For the moment I'am not interest in memtest and similiar more complex ways of doing it, let's keep it simple, thanks.
Last edited on
First: this:
1
2
input>>n>>k;
int array[n];
is not standard C++. gcc allows it as extension to support C99 features. I suggest to turn it off in compiler settings if you want to write portable and standard compliant code.
Proper C++ way will be use of standard containers, for example std::vector<int>
If you insist on arrays you can dynamically allocate your array: int* arr = new int[n];. Then iterate over it and set all elements to 0: for(int i(0); i < n; ++i) arr[i] = 0;. Do not forget to delete your array when you done with it: delete[] arr;
Also you could wait for C++14, partial valarray support and dynamic_array type :)
Topic archived. No new replies allowed.