1
int newBoard[9][9];
int dirtyBoard[9][9];

/*add stuff to every element of dirtyBoard*/
...

newBoard = &dirtyBoard;

I am trying to make my newBoard be an exact copy of cleanBoard. Since performance is a concern I wanted to see if I could bypass creating a loop to copy each element 1 at a time in favor of changing my pointer address or something. Is this possible?

0

6 Answers 6

1

Use memcpy.

memcpy( newBoard , dirtyBoard , sizeof( newBoard )) ;

Don't forget that both arrays must be of the same size, or at least the array you are copying into must be larger.

assert( sizeof( newBoard ) == sizeof( dirtyBoard ) ) ;

If you want to merely point to dirtyBoard use a pointer.

int (*p)[9] = dirtyBoard ; 

Now p behaves almost exactly as dirtyBoard.

Sign up to request clarification or add additional context in comments.

3 Comments

But isn't this the same thing as a for loop that adds each element? Maybe a little more efficient but essentially the same thing? I was really hoping to redirect the variable so when newBoard is called, dirtyBoard is what is really referenced.
Result is the same. Yes. You wanted a copy. Pointing to the same array will not produce a copy.
@self. Just curiosity, "almost exactly" means they are still different? What is left there? Thanks in advance.
1

use memcpy

memcpy(newBoard,dirtyBoard, sizeof(newBoard));

Comments

1

Use memcpy:

memcpy(newBoard, dirtyBoard, sizeof(newBoard);

This will copy sizeof(newBoard) bytes starting at dirtyBoard into newBoard. newBoard must be the same size or bigger than dirtyBoard for this to not cause a buffer overflow.

Comments

0

Arrays don't point. You can't "re-point" an array of ints any more than you can "re-point" a single int.

The name of a variable is only ever associated with the storage allocated to that variable; you can't move a variable around in memory, or repurpose the variable's name to a different variable.

However , pointers point. You can make a pointer that points at an array (or a subset of an array), and then change it to point to a different array.

In your case the syntax would be:

int (*newBoard)[9] = &dirtyBoard[0];

This makes newBoard point at a 1-D array of 9 ints . So then you can use the syntax newBoard[i][j] to access the cells.

Comments

-1

As you have probably found, you cannot do:

newBoard = &dirtyBoard;

The syntax above however will work like this:

int newBoard[9][9];
int* dirtyBoard;
int i, j;
for(i = 0; i < 9; ++i)
   for(j = 0; j < 9; ++i)
       newBoard[i][j] = i*j;

dirtyBoard = newBoard;

However, then you are not copying, you are effectively making an alias. If you update a value in the pointer you will also update the underlying array and vice versa.

If you want to edit the array via the pointer you can do this sort of thing:

#define cols 9
#define rows 9

...
int n;

int newBoard[rows][cols];
int* dirtyBoard;
int i, j;

int row = 3;
int col = 5;

dirtyBoard[row * cols + col] = 3;  /* to flip from 1 dimensional ptr -> [][] form */

With C++ you don't need to use define's you can use:

const int rows = 9;  // and use as array dimensions.

1 Comment

@self What bit of above does not compile?
-1

Since newBoard and dirtyBoard are both statically declared arrays, you can't just point newBoard to the address of dirtyBoard.

If you intend for both to be static arrays, you'll need to copy the data in each element:

for(i=0;i<9;i++)
   for(j=0;j<9;j++)
      newBoard[i][j] = dirtyBoard[i][j];

Edit: As others have mentioned, memcpy() is the way to go, and more efficient.

You could define newBoard as an array of pointer to int, like this:

int (*newBoard)[9];

then you can just do:

newBoard = dirtyBoard;

But, difference here, is that newBoard and dirtyBoard point to the same memory. If you declare two distinct static arrays, you'll end up with two discrete copies of the data.

3 Comments

You cannot do int **newBoard = &dirtyBoard. dirtyBoard is an array of arrays, not an array of pointers.
@MattMcNabb Yes, there are two serious errors in one line.
Wow...where was my brain....sorry, I edited my answer, I think I've got it right now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.