472,990 Members | 2,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

declaring constant array without initializing all the elements

Hi,
I need to create a consant array of larze size but however its
elements can be calculated using an equation, say for example I need
an int arry of 20 elements where each element will be

arr[i] = 2 + (i*i)

But I want arry to be constant. How can I declare such a constant
array without actually defining all the elements?

I could think of one way:
Declare a non const arry

int arr_non_const[20];
for(int i=0; i <20; i++)
{
arr_non_const[i] = 2 + (i*i);
}

Now declare a constant pointer

const int* arr = arr_non_const;

Is it correct? Is there any other way to do so.
Mar 3 '08 #1
2 2978
On Mar 3, 7:10*pm, Pavan <pavan...@gmail.comwrote:
Hi,
* * I need to create a consant array of larze size but however its
elements can be calculated using an equation, say for example I need
an int arry of 20 elements where each element will be

arr[i] = 2 + (i*i)

But I want arry to be constant. How can I declare such a constant
array without actually defining all the elements?

I could think of one way:
Declare a non const arry

int arr_non_const[20];
for(int i=0; i <20; i++)
{
* *arr_non_const[i] = 2 + (i*i);

}

Now declare a constant pointer

const int* arr = arr_non_const;

Is it correct? Is there any other way to do so.
Yes, you can do it that way and expose just 'arr' to the external code
that is supposed to use that array.

You can make the arr_non_const as a global pointer initialized by a
initializer function call which holds the actual array (either static
storage duration or dynamically allocated to increase lifetime) but in
an unnamed namespace and fill it up with whatever logic you have
(initializer function). Then declare the pointer 'arr' as you have
above as a global one as well but you declare it as extern (as in C++
const variables have internal linkage), so as to be accessible in
other places.
Mar 3 '08 #2
On Mar 3, 4:30 pm, Lionel B <m...@privacy.netwrote:

[...]
Now one thing you can do is
const int const_arr = [20] = { 2, 3, 6, 11, 18, ... } // you have to
explicitly initialize the array with the 20 elements
As I understood it, the OP explicitly required that the array
elements "... be calculated using an equation", so I suspect
that this would not work for them.
If the equation is known at compile time, it's pretty easy to
write a program which will generate the definition of the array,
with all of its initializers. I do this a lot (although I do
remember having problems with it in one case: the array
contained a couple of million members, and the compiler wouldn't
handle it.)
One technique which might be made to work if the
initialisation function were simple/suitable, would be some
template meta-programming trick, where you get the compiler to
perform the calculation, the canonical example being a
compile-time calculated factorial:
http://en.wikibooks.org/wiki/C%2B%2B...Template_Meta-
Programming#Example:_Compile-time_Factorial
This would be total overkill, though, I suspect.
It's a nice trick for obfuscation, but generating the code with
a separate program is a lot easier and more readable.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 4 '08 #3

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

Similar topics

7
by: richbl | last post by:
Hello all, I have a question about unserializing a single array element from a serialized array. Can this be done, or must I first unserialize the array, and then access the element? For...
13
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
7
by: Chris Wertman | last post by:
I am so lost on this one. Dim Ary as Integer = New Integer(4) {0,1,2,3} FAILS with the error: Array initializer has 1 too few elements. So I try Dim Ary as Integer = New Integer(3)...
3
by: farseer | last post by:
If i have an array of a certain type, is there away of initializing with without knowing it's type? for example (forgive me if some of this is syntactically incorrect) if i have a procedure...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
11
by: abhiM | last post by:
I have a struct that has an array in it. I need to assign space to the array in a function and pass the corresponding struct by reference to another function so that it can store values into the...
3
by: Steve the Pocket | last post by:
So I'm making a heapsort program... all my sort code seems to work just fine. Only problem is, the part where I create the array to test it bombs out whenever the number of elements is over 100. ...
6
by: Jai Prabhu | last post by:
Hi All, Consider the following piece of code: void func (void) { static unsigned char arr = "\x00\xAA\xBB"; fprintf (stderr, "0x%x\n", arr); fprintf (stderr, "0x%x\n", arr);
8
by: bintom | last post by:
What are the differences between the following methods of declaring strings? char string1 = "C++ forum"; char* string2 = "C++ forum"; I know that the first uses the array notation, whereas...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.