declaring const array trouble...

Feb 25, 2009 at 10:46pm
Heres my problem:

I have a function to generate an array of prime numbers called genprime(), which

returns an array of prime numbers. The first problem is in genprime. I declare an

array, but my compiler gives me a warning because it was declared in the function.

This goes away if genprime() is changed to genprime(long int primes[]), and the

parameter becomes the new array of prime numbers.

How would I go about making the array returned by genprime a const array so I

don't have to recreate it in every function???
Feb 25, 2009 at 10:49pm
Create the array dynamically in your function or use vectors

1
2
3
4
5
6
7
//#include <vector>
vector<long> genprime()
{
     vector<long> temp;
     //your code...
     return temp;
}
Last edited on Feb 25, 2009 at 10:50pm
Feb 26, 2009 at 1:41am
Something like this?

1
2
3
4
5
6
7
8
9
10
const vector<unsigned>& GenPrimes()
{
    static vector<unsigned> primes;

    if ( primes.empty() )
    {
        // generate some primes ...
    }
    return primes;
}

Feb 26, 2009 at 1:07pm
Bazzy's is far better than Skorj's code.
Feb 26, 2009 at 8:32pm
platypus1130's requirement was:

How would I go about making the array returned by genprime a const array so I don't have to recreate it in every function???


I'm guessing that means he doesn't want to re-compute the sequence of primes each time he needs it, which is a problem that Bazzy's code doesn't solve. Of course, I might be misinterpreting the requirement.
Topic archived. No new replies allowed.