2

Possible Duplicate:
How do I use arrays in C++?

I was having trouble copying an array to an array. I have a feeling it may be because of the use of pointers but correct me if I'm wrong.

My function is the following:

bool sodoku::rowTest(sodoku *arr[9][9])
{
  int row = 0;
  while(row < 9)
  {
    for(int j = 0; j < 9; j++)
    {
      for(int k = 0; k < 9; k++)
      {
        if(arr[row][j]->number  == arr[row][j]->possibleNumbers[k])
        {
          for(int i = 0; i < 9; i++)
          {
            arr[row][i]->possibleNumbers[k] = 0;
          }
        }
          for(int g = 0; g < 7; g++)
          {
           int t = 8;
           arr[row][g]->possibleNumbers[k] = arr[row][t]->possibleNumbers[k]; 
          }
          cout << "arr row j num : " << arr[row][j]->possibleNumbers[k] << "row: " << row << " column: " << j << endl;
      }
    }
  row++;
  }
  if(row == 9)
    return true;
  }
return true;
}

My little section of trouble is here:

          for(int g = 0; g < 7; g++)
          {
           arr[row][g]->possibleNumbers[k] = arr[row][8]->possibleNumbers[k]; 
          }

For some reason when I cout each element, the copying doesn't occur. Could anyone help me as to know why this would hhappen? I just want every array from arr[row][1]->possibleNumbers[k] to arr[row][7]->possibleNumbers[k] have the same values as arr[row][8]->possibleNumbers[k]. PossibleNumbers ranges from 0 to 9, if that helps.

If anyone could help that'd be great.

Thanks.

Community
  • 1
  • 1
user1567909
  • 1,450
  • 2
  • 14
  • 24
  • Your use of pointers is fine. I think you're confused by your requirements or your algorithm (or both). You need to use a debugger to step though your code line by line, and see whether what it does actually matches your expectations (and have a big sheet of paper handy too). – john Oct 16 '12 at 10:45
  • 1
    There is something wrong with your code. There is a closing brace too much. – Gorpik Oct 16 '12 at 10:50
  • This is [answered in the array FAQ](http://stackoverflow.com/a/4810672/140719). Voting to close this one. – sbi Oct 16 '12 at 11:23

1 Answers1

3

Array variables are not copied, but you can use std::copy.

Also, passing arrays by value leads to array decay, which means that a lvalue of T(&)[N] actually gets passed as T*. To prevent this, pass by reference instead.

Here is a generic helper function that does this for you:

#include <algorithm>

template <typename T, int N>
void copy(T(&dest)[N], T(&src)[N])
{
    std::copy(dest, dest+N, src);
}

Now you can just say

char dest[5];
char src [5];
copy(dest, src); // provided same element type and size

Note also, that member arrays are copied

struct SudokuRow
{
    unsigned char cells[9];
};

SudokuRow a, b;
a = b; // copies just fine
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Hm interesting. the arrays I'm working with are member arrays but they aren't copying correctly... arr[i][j] is a 2-D set of arrays that have member variables as arrays but they aren't copying correctly. If i did void sodoku::copy(int array1[9], int array2[9]) { for(int i = 0; i < 9; i++) { array1[i] = array2[9]; } } Would that still not work? – user1567909 Oct 16 '12 at 10:52
  • @user1567909 Arrays decay. You cannot pass them by value. Pass by reference (see my code), so `void sodoku::copy(int (&array1)[9], int (&array2)[9]) ` – sehe Oct 16 '12 at 10:56
  • 5
    See also [How do I use arrays in C++?](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) ([tag:c++-faq]) – sehe Oct 16 '12 at 10:57
  • So if i simply did sodoku::copy(int &array1[9], int &array2[9]) { for(int i = 0; i < 9; i++) { array1[i] = array2[8]; } } It would copy the arrays correctly? – user1567909 Oct 16 '12 at 10:58
  • 2
    You **need the parentheses**. Why don't you just ***try the code I've given*** (repeatedly)? – sehe Oct 16 '12 at 11:10