void method1(Board b, int numOfRows, int numOfColumns) { Board bb = copyBoard(b,
ID: 3761687 • Letter: V
Question
void method1(Board b, int numOfRows, int numOfColumns)
{
Board bb = copyBoard(b, numOfRows, numOfColumns);
}
Board copyBoard(Board* b, int numOfRows, int numOfColumns)
{
int i;
int j;
int x;
Board bb=createBoard(b->size);
bb=*b;
return bb;
}
Board createBoard(int s)
{
Board b;
b.size = s;
b.board = (int*)malloc(sizeof(int) * s);
return b;
}
typedef struct
{
int* board;
int size;
}Board;
When I make a copy of Board in method1() and I try to change the original, the copy gets changed as well.. How can I copy each element and be able to change each one without affecting the other?
Explanation / Answer
bb=*b;
This statement is the culprit. New board is basically pointing to old one. To make new board, copy each property manually to bb.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.