Jump to content
  • Advertisement
Sign in to follow this  
gnomer

arrays in functions

This topic is 4683 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Say I want to declare an array inside a function but I don't know what size it will be. The size has to be a constant but even if I pass a constant to the function it doesn't work. I assume this is because it wants to know at compile time what size to reserve for this array before the function is ever called? // assuming size was declared as a const int in main void someFunction( int size ) { int array[size]; } Any tips/tricks to either make this work or tell me the right way I should be handling things like this?

Share this post


Link to post
Share on other sites
Advertisement
Thanks, I'll look into those. Been doing exercises at the end of my books chapter on arrays. Was working out a bucket sort function where I was trying to pass the array size to create the bucket array. Ended up having to use static numbers in the code.

Share this post


Link to post
Share on other sites
Here's some code that demonstrates some really basic ways to work with a std::vector (but definitely read the article about vectors linked above).


#include <vector>
#include <iostream>

using std::cout;
using std::endl;
using std::vector;


int main()
{
vector<int> foo;

// add some elements to foo
foo.push_back(5);
foo.push_back(17);
foo.push_back(3);

// output how many elements are in the vector (how big it is)
cout << "Vector has: " << foo.size() << " elements" << endl;


// now print the values stored in the vector
// actually, it's better to use an iterator to go through the vector
// but i've done it this way to make it more understandable
for (int i = 0; i < foo.size(); ++i)
cout << "Element " << i << ": " << foo << endl;

// output a blank line to make things more readable
cout << endl;

// now remove the last element from the vector
foo.pop_back();
cout << "Vector now has: " << foo.size() << " elements" << endl;

for (int i = 0; i < foo.size(); ++i)
cout << "Element " << i << ": " << foo << endl;

return 0;

}


The output will look like this:
Quote:
Vector has: 3 elements
Element 0: 5
Element 1: 17
Element 2: 3

Vector now has: 2 elements
Element 0: 5
Element 1: 17

Share this post


Link to post
Share on other sites
You'll need to dynamically allocate your arrays if you want to pass in a dynamic value for size (const or not). Dynamic creation doesn't need to know the size at compile time.

void someFunction( int size )
{
int* array = new int[size];
// ... do stuff
delete [] array;
}

Don't forget to clean up after yourself also. Note that the [] is required when deleting if you new'd the object with an [].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!