AllExperts > C++ 
Search      
C++
Volunteer
Answers to thousands of questions
 Home · More C++ Questions · Answer Library  · Encyclopedia ·
More C++ Answers
Question Library

Ask a question about C++
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Eddie
Expertise
I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience
I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

 
   

You are here:  Experts > Computing/Technology > C/C++ > C++ > Question regarding arrays...

C++ - Question regarding arrays...


Expert: Eddie - 1/18/2008

Question
Is there a way to set/change the number of elements within an array at runtime? Everything I've done so far gets an error stating that the number of elements within the array has to be a constant. Thanks in advance for your help!
-Neil

Answer
Hello Neil, thank you for the question.

Sure, you can do that. The easiest way to allocate at runtime is with a pointer. Here is a simple example:

int numItems;
cout << "Enter the number: ";
cin >> numItems;

int *array = new int[numItems];

// do something with the array
for(int i = 0; i < numItems; i++)
{
array[i] = i;
}

// clean up
delete [] array;

That should have you pointing in the right direction.

I hope this information was helpful.

- Eddie

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.