User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,850 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,305 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1416 | Replies: 6 | Solved
Reply
Join Date: Mar 2007
Location: Honduras
Posts: 1,401
Reputation: Nichito is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 25
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

user given array size

  #1  
Jul 7th, 2007
i know i learned it somewhere, but its been a while since i used it... how do you make an user given array size... i mean...
int b;
cout<<"Input the array size: ";
cin>>b;
int array[b];

i know this is not hte way of doing it... but just so you get the idea of what i need to do...
-->sometimes i wanna take my toaster in a bath<--
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,340
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 378
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: user given array size

  #2  
Jul 7th, 2007
int *array = new int[b];
Or if you can, use a std::vector
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Oct 2006
Location: NY
Posts: 218
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Rep Power: 3
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: user given array size

  #3  
Jul 7th, 2007
Originally Posted by Nichito View Post
i know i learned it somewhere, but its been a while since i used it... how do you make an user given array size... i mean...
int b;
cout<<"Input the array size: ";
cin>>b;
int array[b];

i know this is not hte way of doing it... but just so you get the idea of what i need to do...

arrays have to be constant
maybe:
const int size = b;
int array[size];
Of coarse, once it's done, you can't re-size it.

why not use a vector?
Reply With Quote  
Join Date: Mar 2007
Location: Honduras
Posts: 1,401
Reputation: Nichito is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 25
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: user given array size

  #4  
Jul 7th, 2007
Originally Posted by JRM View Post
arrays have to be constant
maybe:
const int size = b;
int array[size];
Of coarse, once it's done, you can't re-size it.

why not use a vector?

did you try it? 'cuz when i did that, it sent me the error Error 13: Constant expression required

i don't use a vector because it is not what i need...
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote  
Join Date: Mar 2007
Location: Honduras
Posts: 1,401
Reputation: Nichito is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 25
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: user given array size

  #5  
Jul 7th, 2007
thnx for your help...
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote  
Join Date: Oct 2006
Location: NY
Posts: 218
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Rep Power: 3
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: user given array size

  #6  
Jul 7th, 2007
Originally Posted by Nichito View Post
did you try it? 'cuz when i did that, it sent me the error Error 13: Constant expression required

i don't use a vector because it is not what i need...


I just looked it up. Arrays cannot be intialized with a non-const value or a value unknown at compile time. That's why it failed.
You will have to initailize it with the largest possible size required.
Perhaps use a pointer with a limlit set in a loop by a user determined variable might be the way to go?

I don't know WHY you need to do thsi, so it's hard to guess what other methods might be appropriate.

Good luck!
Reply With Quote  
Join Date: Feb 2006
Location: UK
Posts: 468
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Rep Power: 5
Solved Threads: 42
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: user given array size

  #7  
Jul 7th, 2007
Originally Posted by JRM View Post
I don't know WHY you need to do thsi, so it's hard to guess what other methods might be appropriate.
For a statically allocated array, the size must be known at compile time - therefore there's no reason to allow a non-const variable to be used to declare an array.

Allowing such behaviour inevitably invites beginners to try such things as the following, which, if it weren't illegal, would be undefined behaviour
  1. int main()
  2. {
  3. int size(5);
  4. int arr[size] = { 1,2,3,4,5 };
  5. size = 8;
  6. arr[5] = 6;
  7. }
Disallowing non-const variables to be used in static array declarations adds a compile-time check to help prevent this kind of code appearing.

There may be other reasons the ISO standards committee decided to disallow it, but this seems reason enough to me.
Last edited by Bench : Jul 7th, 2007 at 1:24 pm.
¿umop apisdn upside down?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 8:07 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC