Home > Net >  Copy elements of int * * array (2D) to another 2D array
Copy elements of int * * array (2D) to another 2D array

Time:11-12

I found memcpy() works for characters on stackoverflow, but there are no good answers for copying the contents of a 2D array to another one.

Say we have to copy contents of a board game represented in a 2D array, can this be done through dereferencing like in the case of normal variables? and if not how's the way to do so? Here's some code for context:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <assert.h>

int main() {
    //copying var using dereferencing
    int x = 7;
    int copy;
    int *copyX = &copy;
    *copyX = x;

    printf("%d", copy);

    //copying array using dereferencing??
    int **board1 = createBoard();
    insert(board1, 2, 1);
    insert(board1, 5, 2);
    int **board2 = createBoard();
    *board2 = board1;   //can we do this
}

//Requires: nothing.
//Effects: returns a game board initialized with 0s (pointer to 2D array).
int **createBoard() {
    //Allocate memory
    int **a = (int **)malloc(6 * sizeof(int *));
    for (int i = 0; i < 6; i  )
        a[i] = (int *)malloc(7 * sizeof(int));
    //Initialize with zeros
    for (int i = 0; i < 6; i  ) {
        for (int j = 0; j < 7; j  ) {
            a[i][j] = 0;
        }
    }
    return a;
}

//Requires: pointer to the board to be printed.
//Effects: prints the current state of the game board.
void printBoard(int **board) {
    printf("- - - - - - - - - - - - - - - \n");

    for (int i = 0; i < 6; i  ) {
        printf("| ");
        for (int j = 0; j < 7; j  ) {
            printf("%i | ", board[i][j]);
        }
        printf("\n");
    }
    printf("- - - - - - - - - - - - - - - \n");
}

//Requires: pointer to the board , column to insert token in, and current player (1 for red, 2 for yellow).
//Effects: inserts token in  the game board.
int insert(int **board, int columnNb, int playerTurn) {
    if (columnNb < 0 || columnNb >= 7) {
        printf("Illegal Move (out of bounds) \n");
        return -1;
    } 
    if (board[0][columnNb] != 0) {
        printf("Illegal Move (full column) \n");
        return -1;
    } else {
        for (int i = 5; i >= 0; i--) {
            if (board[i][columnNb] == 0) { //found the empty cell
                board[i][columnNb] = playerTurn; //insert token
            }
        }
    }
    return 0;
}

I tried dereferencing, but things are complicated because of malloc() & I am confused on how copying happens.

CodePudding user response:

In this expression statement

* board2= board1;

the left operand has the type int * while the right operand has the type int **. So the compiler will issue a message because there is no implicit conversion between these pointer types.

Pay attention to that you do not have two dimensional arrays. You have two sets of one-dimensional arrays.

If you want to use memcpy then you need to use a loop as for example

#include <string.h>

//...

for ( int i = 0; i < 6; i   )
{
    memcpy( board2[i], board1[i], 7 * sizeof( int ) );
}

After the for loop the both sets of arrays will contain identical values.

  • Related