# Thread: Trying to assign array values to another array

1. ## Trying to assign array values to another array

Hey all. I'm new to C++ programing, although not stranger to programing at all (I have a lot of experience in Java and PHP). Anyway, I'm trying to assing values of two-dimensional array to a previously declared array. I'm using the basic formula:

Let's suppose the array with the values I wanna copy are:
Code:
`int array1[3][3] = {{1,2,3},{4,5,6},{7,8,9}};`
and then I have the other array:
Code:
`int array2[3][3];`
The obvious way of doing it would be:
Code:
```for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
array2[i][j] = array1[i][j];
}
}```
Yeah, but it ain't working. I'll try to explain my best the results:
When i = 0 and j = 0, array2 value becomes {{1,0,0},{1,0,0},{1,0,0}}. See the problem already? It assings array1[0][0] to array2[0][0], array2[1][0] and array2[2][0], whereas it should assing it to array2[0][0] only. By the time it reaches i = 0 and j = 2, array2 will be {{1,2,3},{1,2,3},{1,2,3}}.
When the program has i = 1 and j = 0, it array2 will be {{4,2,3},{4,2,3},{4,2,3}}. You see? It assigned array1[1][0] to array2[0][0], array2[1][0] and array[2][0] again! By the time i = 2 and j = 2, array2 = {{7,8,9},{7,8,9},{7,8,9}}.

If you think of both arrays as matrices, when an element is assinged from the first matrix to the second, it is assigned to all lines in that collumn instead of the desired line and collumn.

So, I kindly ask you guys: WHAT THE HELL IS GOING ON?

2. It looked correct to me so I tested with:
Code:
```#include <iostream>

int main()
{
int array1[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int array2[3][3];

for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
array2[i][j] = array1[i][j];
}
}

for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
std::cout << array2[i][j] << ' ';
}
}
std::cout << std::endl;
}```
The output was as expected:
1 2 3 4 5 6 7 8 9

I suggest you post the smallest and simplest compilable program that demonstrates the error.

3. Alright! Tried to mimick my code with the least amount of complexity and found out what the problem was. It is related to inheritance.

Anyway, here's the deal:

array2 is declared but not initialized in a base class. Several derived classes will carry what the array dimensions will be and since I can't declare the an array without dimension, I'm declaring it as
Code:
```//base.h
int array2[0][0];```
I wanna assign those values (dimensions) to it in the derived class, so what I was trying to do was
Code:
```//derived.h
int array2[3][3];```
And that caused the problem, when I changed the dimensions of the array in the base class to match those in the derived class, it worked.

So my actual problem is: how can I overwrite this array in the derived class with new dimensions? I need it declared in the base class since I don't wanna redefine or move some functions in the base class to the derived class that use this array (although I could do that and it would probably work), but the only way to assign its dimension and values is in the derived class.

4. So my actual problem is: how can I overwrite this array in the derived class with new dimensions? I need it declared in the base class since I don't wanna redefine or move some functions in the base class to the derived class that use this array (although I could do that and it would probably work), but the only way to assign its dimension and values is in the derived class.
You probably should use dynamically allocated arrays here, or better yet, a vector of vectors.

5. Originally Posted by edhc44
So my actual problem is: how can I overwrite this array in the derived class with new dimensions?
You can't do that. If you define a variable with the same name in a derived class, it will only hide the one in the base class (read: there will be two variables named the same name, but the derived can only access its own variable and the base can still access its own). So there will be two variables with different dimensions but same name. So what you need to do is, like laserlight points out, to use dynamic memory, new and delete. And preferably also a smart pointer such as boost's shared_ptr.

6. Originally Posted by Elysia
You can't do that. If you define a variable with the same name in a derived class, it will only hide the one in the base class (read: there will be two variables named the same name, but the derived can only access its own variable and the base can still access its own). So there will be two variables with different dimensions but same name.
So, there's one thing I'm already doing wrong...

I will post the compilable code I made to help others understand my problem:

Code:
```//base.h
class base{

public:
virtual void createArray();

protected:
int array[3][3];
};```
Code:
```//derived.h
#include "base.h"
class derived:public base{

private:
int array[3][3];

};```
Code:
```//derived.cpp
#include "derived.h"
#include <iostream>

void base::createArray(){
int temp[3][3] = {{1,2,3},{4,5,6},{7,8,9}};

for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
this->array[i][j] = temp[i][j];
}
}

for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
std::cout << this->array[i][j] << ' ';
}
}
std::cout << std::endl;
}```
Code:
```//main.cpp
#include <iostream>
#include "derived.h"

derived *test = new derived();

int main()
{
test->createArray();
}```
This code runs just fine. Although I think I made myself clear in my last post, I'd like to point some things out:
1) The dimension "3" in the derived class is just an example. Every derived class can have this dimension different from one another.
2) The dimension "3" in the base class is only there in order to match the dimension in the derived class and make this example code run.

So there you go, I don't know for sure what the dimension will be when I declare private: int array[x][y] in the base class, how can I use dynamically allocated arrays in this situation? Considering what Elysia said, it's worthless declare array AGAIN in the derived class, but as I said before, it is the derived class that actually have those dimensions...

Maybe this has an easy, simple solution, but as I said, I'm new to C++...

PS: I actually have a good reason not to use vectors and that's why I'm insisting on arrays.

7. Simply add a pointer to the base class and let all the derived classes inherit it and allocate the memory necessary for your arrays.

8. I would go with the suggestion of using a vector of vectors, that way, you don't have to worry about how they are allocated and such. But a 2D array allocation function looks something like this:

Code:
```int **Alloc2D(int rows, int cols)
{
int **ptr2D;
ptr2D = new int* [rows];
for(int i = 0; i < cols; i++)
ptr2D[i] = new int [cols];
return ptr2D;
}

int main()
{
int **array2d;
int temp[3][3] = {{1,2,3},{4,5,6},{7,8,9}};

array2d = Alloc2D(3, 3);

for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
array2d[i][j] = temp[i][j];
}
}

return 0;
}```

9. Originally Posted by matsp
I would go with the suggestion of using a vector of vectors, that way, you don't have to worry about how they are allocated and such. But a 2D array allocation function looks something like this:

Code:
```int **Alloc2D(int rows, int cols)
{
int **ptr2D;
ptr2D = new int* [rows];
for(int i = 0; i < cols; i++)
ptr2D[i] = new int [cols];
return ptr2D;
}

int main()
{
int **array2d;
int temp[3][3] = {{1,2,3},{4,5,6},{7,8,9}};

array2d = Alloc2D(3, 3);

for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
array2d[i][j] = temp[i][j];
}
}

return 0;
}```
Thanks dude, problem solved! ^^

10. Originally Posted by edhc44
Code:
```//base.h
int array2[0][0];```
You can't have an array with 0 dimension. Turn up your compiler warnings.

11. PS: I actually have a good reason not to use vectors and that's why I'm insisting on arrays.
What was the reason anyway?