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.
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.
Comment