Is it possible to have a dynamic array size?
For example:
It wont compile but i was wondering if it's possible to achieve that some way.Code:... int size; cout<<"size: "; cin>>size; cin.ignore(); int array[size]; ...
Thanks!
Is it possible to have a dynamic array size?
For example:
It wont compile but i was wondering if it's possible to achieve that some way.Code:... int size; cout<<"size: "; cin>>size; cin.ignore(); int array[size]; ...
Thanks!
"Anyone can aspire to greatness if they try hard enough."
- Me
A std::vector?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Oh... ha.
Never used a vector before.
I'll have to look into that i suppose
Thanks!
"Anyone can aspire to greatness if they try hard enough."
- Me
If you want to have the user input a value that will serve to initialize the array only once, you can dynamically allocate memory using the new operator. If you want to change the array's size you want an std::vector or an std::list.
Code:int* array = new array[size] // ... // not to forget ! delete[] array; array = 0;
Even if you don't need to change the array size, it is generally better to use a vector or other container in C++.