473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

declare const array

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 '07 #1
2 15551
vi**************@gmail.com 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
Apr 23 '07 #2
<vi**************@gmail.comwrote in message
news:11**********************@b58g2000hsg.googlegr oups.com...
: 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<doubledata( (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[i] 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

Apr 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dylan Nicholson | last post by:
Is there any way of declaring the parameter "array" below to be const, so that the function as written will not compile: void foo(int array /*const*/) { array = array; // should not be allowed...
5
by: Ingo Brueckl | last post by:
I need to declare a fixed array of (already defined and working) sort functions (sortfunc1, sortfunc2, sortfunc3) to pass one element of the array to qsort itself: int index; ??? func =...
4
by: George Ter-Saakov | last post by:
I need to declare const 2 dimensional array something like string topmenu = { {"menu1", "aaa"}, {"menu2", "bbb"} } Thanks. George.
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
3
by: johnmmcparland | last post by:
Hi all, I would like to have a static constant array inside a class definition which would contain the number of days in each month (I am writing a Date class as an exercise). However my...
1
by: peary | last post by:
Hi, everyone, I'm writing a program to discover wireless network using Windows Native Wifi API & VB.net. I have to declare the windows API in my VB.net program. The original windows...
10
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
10
by: Tammy | last post by:
Hello all, I am wondering what is the best way to declare a struct to be used in other c and c++ files. Such as for a C API that will be used by others. 1. Declaring the typedef and the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.