declare const array

Discussion in 'C++' started by vishwesha.guttal@gmail.com, Apr 23, 2007.

  1. Guest

    Hi,

    I am having troble declaring a const array. If the array size is
    small, then one can do as follows:

    const double array[5] = {1, 2, 3, 4, 5};

    What if I have an array of size say 1000 or 10000 which, lets say, I
    am reading from a data file? Then manually entering the values is
    impossible. So how do I declare such a constant array?

    thanks..
    Vishwesha
    , Apr 23, 2007
    #1
    1. Advertising

  2. wrote:
    > I am having troble declaring a const array. If the array size is
    > small, then one can do as follows:
    >
    > const double array[5] = {1, 2, 3, 4, 5};
    >
    > What if I have an array of size say 1000 or 10000 which, lets say, I
    > am reading from a data file? Then manually entering the values is
    > impossible. So how do I declare such a constant array?


    When are you readin ghtme from a data file? Run-time?

    If you're concerned with protecting the contents from some accidental
    change, you could use the reference trick:

    double my_non_const_array[100000];
    int dummy = read_array_from_file(my_non_const_array);
    double const (&array)[100000] = my_non_const_array;

    which will still allow you to use 'array' in your C++ code, and the
    type of elements will be 'const double'.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 23, 2007
    #2
    1. Advertising

  3. <> wrote in message
    news:...
    : I am having troble declaring a const array. If the array size is
    : small, then one can do as follows:
    :
    : const double array[5] = {1, 2, 3, 4, 5};
    :
    : What if I have an array of size say 1000 or 10000 which, lets say, I
    : am reading from a data file? Then manually entering the values is
    : impossible. So how do I declare such a constant array?

    Use an std::vector to store data read from the file, then use
    a "const-pointer to const" referring to its first element.

    Consider:

    #include <vector>
    #include <fstream>
    #include <iterator>
    #include <cstddef>

    int main()
    {
    std::ifstream src("srcFileName");
    std::vector<double> data( (std::istream_iterator<double>(src)),
    std::istream_iterator<double>() );

    double const* const pItems = &data.front();
    std::size_t nItems = data.size();

    for( std::size_t i=0 ; i<nItems ; ++i )
    {
    // use value of pItems however you'd like
    }
    }

    hth, Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
    Ivan Vecerina, Apr 24, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Ter-Saakov

    how do i declare const array?

    George Ter-Saakov, Aug 22, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    3,635
    George Ter-Saakov
    Aug 22, 2003
  2. Dylan Nicholson

    Declare an array parameter to be const?

    Dylan Nicholson, Jan 27, 2005, in forum: C++
    Replies:
    6
    Views:
    5,219
    Andrey Tarasevich
    Jan 27, 2005
  3. Replies:
    11
    Views:
    1,099
  4. Javier
    Replies:
    2
    Views:
    559
    James Kanze
    Sep 4, 2007
  5. 0m
    Replies:
    26
    Views:
    1,111
    Tim Rentsch
    Nov 10, 2008
Loading...

Share This Page