Basically the title. I'm trying to change the values of newArray in the Copy2DArray Function to be the same as originalArray, but I keep getting the error "expression must be a modifiable lvalue". I've been searching around but I can't see why this isn't working while similar stuff with one dimensional arrays works fine, so help will be appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
constint COLUMNS = 3;
constint ROWS = 4;
void Copy2DArray(int theOriginalArray[][COLUMNS], int newArray[][COLUMNS]) {
int row, col;
for (row = 0; row <= ROWS - 1; row++) {
for (col = 0; col <= COLUMNS - 1; col++) {
newArray[row, col] = theOriginalArray[row, col];
}
}
}
int main()
{
int originalArray[ROWS][COLUMNS] = { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12} };
int newArray[ROWS][COLUMNS];
Copy2DArray(originalArray, newArray);
}
constint COLUMNS{ 3 };
constint ROWS{ 4 };
void Copy2DArray(int theOriginalArray[][COLUMNS], int newArray[][COLUMNS])
{
//int row, col; // <--- Not here.
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLUMNS; col++)
{
newArray[row][col] = theOriginalArray[row][col];
}
}
}
int main()
{
int originalArray[ROWS][COLUMNS]
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
int newArray[ROWS][COLUMNS];
Copy2DArray(originalArray, newArray);
return 0;
}
If your IDE/compiler is setup to use the C++11 standards Lines 1 and 2 are the same as what you started with. Also empty {}s will initialize a variable to (0) zero.
Line 6 if you need these variables outside the for loop this is OK otherwise they should be defined in the for loop.
In the for loops I have found it is better to avoid using "<=" as this tends to give you one extra loop than you want. When you think about it the array is defined as (4) rows this would be (0 to 3) and columns as (3) and this would be (0 to 2). So in the for loop row < ROWS means (0 to 2) is true and 3 is false. Once you get use to this you will find it works out better.
For line 12 your use of the comma operator does not work the way that you think. Most likely you are using "col" as the subscript for the first dimension.
In "main" defining and initializing the array I like this method as it gives a nice visual representation of the array. This can also be helpful when working with the array looking at something that makes sense.
The return statement is not necessary, but it does make a good break point when debugging the program.
and, what I gave you is kinda C-ish. But so are arrays, so call it even. The ability to say a = b for C++ vectors is one of their top 10 coolest features in my book. As soon as you can get to replacing C arrays with vectors, you won't need so many of these kinds of things. Memcpy is old school dangerous -- GIGO type stuff that has zero safety features. Its good at what it does, though :)
Do not be so hard on yourself. The trouble with being new is a multiplied effect -- you don't know what is out there, so you don't know to look for it, and if you did suspect it was out there, getting the right words in the search to make it pop up is also tricky. In time both the terms and the suspicion of what is out there will mesh and you will be able to find things quickly.