Reusing arrays

Hello again, are there any co
nsiderations when reusing arrays?. By reusing I mean assigning a new set of values after already declaring and using once?

Int B1 = {3,4,5,6,7}; // declared and used once

Then ideally I would like to do something like this

B1[]={10,12,13,15,20}; // my compiler doesn't like this?

Not B1[0]=10;
B1[1]=12;
Etc

Or do I use pointers in some way?
You can only use the initialiser list {3,4,5,6,7, ...} once when you define your array. After that you need to assign values individually:

1
2
3

B1[2] = 7;
Last edited on
Alternatively you can use a temporary array, then copy the values back into the original, and hope the optimizer can inline the std::copy call, and unroll the loop.
1
2
3
4
int a[3] = {1, 2, 3};
//...
const int temp[3] = {4, 5, 6};
std::copy(temp, temp+3, a);

Or wait for the new C++0x standard, and use std::initializer_list.
I'm quite surprised by that?? ,I thought there would be a way, just I didn't know how!
Can the addresses of each element in the array be accessed by pointer manipulation?
you can copy elements of one data onto other by following way -
1
2
3
4
5
6
7
int array[5] = {1,2,3,4,5};
int temp[5] = {6,7,8,9,10};

for(int i =0;i<5;i++)
{
array[i] = temp[i];
}


B1[]={10,12,13,15,20}; // my compiler doesn't like this?
That is illegal, when declaring an array you must specify the size which would remain fixed throughout the prorgam.

Why don't you use vector...
Last edited on
Topic archived. No new replies allowed.