473,624 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15587
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_ar ray[100000];
int dummy = read_array_from _file(my_non_co nst_array);
double const (&array)[100000] = my_non_const_ar ray;

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.comwro te in message
news:11******** **************@ b58g2000hsg.goo glegroups.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("srcFileNam e");
std::vector<dou bledata( (std::istream_i terator<double> (src)),
std::istream_it erator<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
10818
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 to point this at another array! }
5
2878
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 = {sortfunc1, sortfunc2, sortfunc3}; index = /* calculated somehow */; qsort(what, what_elements, sizeof(what_element), func);
4
1855
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
3839
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? Here's what I thought should work, but apparently doesn't: class Foo; void f1(Foo* p)
8
10157
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". */ unsigned short int array_size = 25; /*Declare an array named "numbers" using the variable initialized above. */ unsigned short int numbers;
3
20863
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 attempts so far have been unsuccessful. Take this Test class as an example // test.hpp
1
2121
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 declaration is as below. The problem is in "struct _WLAN_AVAILABLE_NETWORK_LIST", it declared "WLAN_AVAILABLE_NETWORK Network;". I think which means a Network array of struct WLAN_AVAILABLE_NETWORK.
10
5949
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 is unused, I know that it does not take up storage in the
10
2470
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 struct in the header file and including this file in all source files that need it? For example: mystruct.h
0
8240
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8336
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7168
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2610
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.