dynamic arrays
Sep 11, 2009 at 6:24am UTC
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];
Sep 11, 2009 at 7:34am UTC
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 Sep 11, 2009 at 7:35am UTC
Sep 11, 2009 at 8:43am UTC
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 -
Sep 11, 2009 at 5:15pm UTC
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
Sep 11, 2009 at 10:08pm UTC
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.