dynamic arrays

OK, the following works on Dev-CPP but on Visual it throws up erros saying size of array unknown? Why?
1
2
3
4
int size;
cout<<"\nEnter Size: ";
cin>>size;
char list[size];
You can't declare an array with a variable. The storage for an arrray is set aside at compile time. You're relying on run time information.

You probably want to use a vector.
1
2
3
4
size_t size = 0;
std::cout << "\nEnter Size: ";
std::cin >> size;
std::vector<char> list(size);
Last edited on
If you want dynamic array you need to use new:
1
2
3
4
5
6
7
int size;
cout<<"\nEnter Size: ";
cin>>size;
char *list = new char[size];

//after you used the array:
delete[] list;
-Vectors and other containers are better than dynamic arrays -
i agree, but the assignment is to use arrays. I actually got it to work like this (yeah, it's a two dimensional now):
1
2
3
4
int size;
char** list;
cin>>size;
char ** list=new char [size][80];


Thanks for the help guys. Still, I wonder how it worked on Dev-Cpp without the pointers
dev-cpp must have non-std extensions enabled or simply isn't following the ISO C++ std. Any compiler vendor can write useful extensions to the C++ language. Typically those additional features can be enabled or disabled.
Topic archived. No new replies allowed.