how can i write a variable sized array...for instance:
1 2 3
int x;
in >> x;
int array[x];
now, that wont compile on my compiler (Borland turbo c++). can anyone help me and tell me how i can create an array that is of a variable size?
my compiler gives the error that it cant convert int into int* (i.e, it cant convert the x into the array[x])
thanks for the help
I believe you might be using an older compiler that does not support variable sized arrays, so you'll need to dynamically create it as Somelauw suggested.
1 2 3 4 5
int x;
in >> x;
int* array = newint[ x ];
// use array here ...
delete [] array;
int *myArray; //Declare pointer to type of array
myArray = newint[x]; //use 'new' to create array of size x
myArray[3] = 10; //Use as normal (static) array
...
delete [] myArrray; //remeber to free memeory when finished.
For more sophisicated use of dynamic data structures check the STL container classes (http://www.cplusplus.com/reference/stl/) once you are happy you understand the standard dynamic arrays.
thanks for all the help - im using Borland Turbo c++, but possibly using Dev c++ might fix this?
and will using int* array = newint [ x ]; rewrite the original x variable?