Search code examples
arrayscarduinoassignment-operatorarduino-c++

Assigning a 2d-matrix to another variable in C++/Arduino


I have an array or matrix like first[][4] = {{7, 255, 0, 0}, {6, 0, 255, 0}} and another variable. How can I assign the 2d-matrix to the variable, and can I even do it? I have some more 2d-matrices that look like first shown here, and I want for var to be assigned by one of them.


Solution

  • Initially your question had the C tag. So I will start bearing in mind C.

    Arrays do not have the assignment operator. If you want to copy elements of one array into another you should use either the standard function memcpy declared in the header <string.h> or use "manually" written loops.

    Here is a demonstrative program

    #include <stdio.h>
    #include <string.h>
    
    int main(void) 
    {
        enum { M = 2, N = 4 };
        
        int first[M][N] = 
        {
            { 7, 255, 0, 0 }, 
            { 6, 0, 255, 0 }
        };
        int second[M][N];
        
        memcpy( second, first, M * N * sizeof( int ) );
        
        for ( size_t i = 0; i < M; i++ )
        {
            for ( size_t j = 0; j < N; j++ )
            {
                printf( "%3d ", second[i][j] );
            }
            putchar( '\n' );
        }
        
        return 0;
    }
    

    The program output is

      7 255   0   0 
      6   0 255   0 
    

    On the other hand you can assign an array to a pointer. For example

    int first[][4] = {{7, 255, 0, 0}, {6, 0, 255, 0}};
    int ( *p )[4] = first;
    

    or

    int first[][4] = {{7, 255, 0, 0}, {6, 0, 255, 0}};
    int ( *p )[sizeof( first ) / sizeof( *first )][4] = &first;
    

    and use the pointers to access elements in the array.

    Another approach is to wrap an array in a structure. In this case you will be able to assign one array to another using the assignment operator for structures.

    Here is a demonstrative program.

    #include <stdio.h>
    #include <string.h>
    
    int main(void) 
    {
        enum { M = 2, N = 4 };
        struct Array
        {
            int a[M][N];
        };
        
        struct Array first =
        {
            {
                { 7, 255, 0, 0 }, 
                { 6, 0, 255, 0 }
            }           
        };
        struct Array second;
        
        second = first;
        
        for ( size_t i = 0; i < M; i++ )
        {
            for ( size_t j = 0; j < N; j++ )
            {
                printf( "%3d ", second.a[i][j] );
            }
            putchar( '\n' );
        }
        
        return 0;
    }
    

    Its output is the same as shown above that is

      7 255   0   0 
      6   0 255   0 
    

    If you are using C++ then you may use a standard container as for example std::array or std::vector or even a combination of them for multi-dimensional arrays.

    Here is a demonstrative program that uses std::array.

    #include <iostream>
    #include <iomanip>
    #include <array>
    
    int main() 
    {
        std::array<std::array<int, 4>, 2>  first =
        {
            {
                { { 7, 255, 0, 0 } }, 
                { { 6, 0, 255, 0 } }
            }           
        };
    
        std::array<std::array<int, 4>, 2>  second;
        
        second = first;
        
        for ( const auto &row : second )
        {
            for ( const auto &item : row )
            {
                std::cout << std::setw( 3 ) << item << ' ';
            }
            std::cout << '\n';
        }
        
        return 0;
    }
    

    As usual the program output is

      7 255   0   0 
      6   0 255   0