I want to copy 2d array and assign it to another.
In python i will do something like this
grid = [['a','b','c'],['d','e','f'],['g','h','i']]
grid_copy = grid
I want to do same in C.
char grid[3][3] = {{'a','b','c'},{'d','e','f'},{'g','h','i'}};
How do i copy this array to copy_grid ?
Use memcpy
standard function:
char grid[3][3] = {{'a','b','c'},{'d','e','f'},{'g','h','i'}};
char grid_copy[3][3];
memcpy(grid_copy, grid, sizeof grid_copy);
Use memcpy , don't forget to include <string.h>
#include <string.h>
void *memcpy(void *dest, const void *src, size_t n);
Or, do it manually using loop put each value one by one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With