'How to assign values of array to another array(making copy) in c?

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 ?



Solution 1:[1]

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);

Solution 2:[2]

Although the answers are correct, one should remark that if the array is inside a structure, assigning structures will copy the array as well:

struct ArrStruct
{
    int arr[5][3];
};

int main(int argc, char argv[])
{
    struct ArrStruct as1 = 
    { { { 65, 77, 8 }, 
    { 23, -70, 764 }, 
    { 675, 517, 97 }, 
    { 9, 233, -244 }, 
    { 66, -32, 756 } } };

    struct ArrStruct as2 = as1;//as2.arr is initialized to the content of as1.arr

    return 0;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ouah
Solution 2 Andrey Pro