Author |
Message |
AsianSensation
|
Posted: Sun Apr 04, 2004 9:18 pm Post subject: arrays |
|
|
it seems that I can't declare an array with a variable as it's upper bound. Even if I declare the variable to be a constant, it still won't let me declare the array.
code: | int boundry = 7;
string name [boundry]; |
code: | const int boundry = 7;
string name [boundry]; |
both of the above doesn't seem to work. So, is there an easy way to declare an array with unknown boundry? Right now I'm using linked lists, just wondering if there is an easier way. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Sun Apr 04, 2004 9:22 pm Post subject: (No subject) |
|
|
I'm preaty sure that I've read that in C++ array size must be decleared at compile time, so you can't use variables ![Confused Confused](http://compsci.ca/v3/images/smiles/icon_confused.gif) |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
AsianSensation
|
Posted: Sun Apr 04, 2004 9:23 pm Post subject: (No subject) |
|
|
so I'm stuck with linked lists? that's not remotely cool. I really don't want to make bunch of recursive functions for a small task like this.... |
|
|
|
|
![](images/spacer.gif) |
Catalyst
![](http://catalyze.mine.nu/jack_of_spades+100.jpg)
|
Posted: Sun Apr 04, 2004 9:44 pm Post subject: (No subject) |
|
|
code: |
float *array;
int sizeOfArray=12;
array=new float[sizeOfArray];
// Do Something
delete [] array;
|
|
|
|
|
|
![](images/spacer.gif) |
AsianSensation
|
Posted: Sun Apr 04, 2004 10:07 pm Post subject: (No subject) |
|
|
oh, cool, thanks Catalyst. |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Wed Apr 07, 2004 12:00 am Post subject: (No subject) |
|
|
Well, they're sort of flexible. You can't resize them after initializing.
If you need something resizeable, you can use the vector class that's already been written.
code: | #include <vector>
int main()
{
std::vector<int> my_vector(boundary);
// Then, to add something onto the end...
my_vector.push_back(42);
} |
|
|
|
|
|
![](images/spacer.gif) |
Andy
|
Posted: Mon Apr 12, 2004 3:30 pm Post subject: (No subject) |
|
|
cant u just run a for loop through the subsets after the upperbound and declare them to null? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
bugzpodder
![](http://www.vbforums.com/avatar.php?userid=31755&dateline=1038631511)
|
Posted: Wed Apr 14, 2004 7:06 pm Post subject: (No subject) |
|
|
Quote: Even if I declare the variable to be a constant, it still won't let me declare the array.
sure it would. except what you did isnt technically a constant
try something like const int MAXSIZE=1000 or #define MAXSIZE 1000 |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Wed Apr 14, 2004 7:11 pm Post subject: (No subject) |
|
|
bugzpodder wrote: try something like const int MAXSIZE=1000 or #define MAXSIZE 1000
The former is the preferred method. |
|
|
|
|
![](images/spacer.gif) |
|