Search code examples
c++pointersmultidimensional-arrayreinterpret-castfunction-declaration

How to pass in a 3D array with set x index as a 2D array?


I have a 3D array arr[x][y][z], where at a given point x is a constant, and I want to pass in are[const][y][z] as a 2D pointer. The following lines are how I attempted to do so:

double tmpMatrix[msize][msize][msize];<- array declaration

...

test(msize, (double*)(tmpMatrix[i]));<- function calling

...

void test(int msize, double * m) <- function which takes in 2D arrays

This is my first question on stack overflow, if there are any useful tips you could provide me, it would be much appreciated. Any unnecessary hate will be ignored.


Solution

  • There is a wrong comment to this function declaration

    void test(int msize, double * m) <- function which takes in 2D arrays
    

    Neither parameter of the function is declared as a two dimensional array or a pointer to a two-dimensional array.

    It seems you mean

    void test(int n, double ( * m )[msize][msize] ); <- function which takes in 2D arrays.
    

    Where msize - is a compile-time constant.

    Or if you want to pass a pointer to a two-dimensional array as a pointer to one dimensional array then you have to write

    test( ( msize - x ) * msize * msize, reinterpret_cast<double *>( tmpMatrix + x ) );
    

    Here is a demonstrative program I changed the order of the parameter declarations in the function test.

    #include <iostream>
    
    const size_t msize = 3;
    
    void test( double *a, size_t n )
    {
        for ( size_t i = 0; i < n / ( msize * msize ); i++ )
        {
            for ( size_t j = 0; j < msize; j++ )
            {
                for ( size_t k = 0; k < msize; k++ )
                {
                    std::cout << a[i * msize * msize + j * msize + k] << ' ';
                }
                std::cout << '\n';
            }
            std::cout << '\n';
        }
    }
    
    int main() 
    {
        double a[msize][msize][msize] =
        {
            {
                { 1.1, 2.1, 3.1 },
                { 4.1, 5.1, 6.1 },
                { 7.1, 8.1, 9.1 }
            },
            {
                { 1.2, 2.2, 3.2 },
                { 4.2, 5.2, 6.2 },
                { 7.2, 8.2, 9.2 }
            },
            {
                { 1.3, 2.3, 3.3 },
                { 4.3, 5.3, 6.3 },
                { 7.3, 8.3, 9.3 }
            },
        };
        
        for ( size_t i = 0; i < msize; i++ )
        {
            test( reinterpret_cast<double *>( a + i ), ( msize - i ) * msize * msize );
            putchar( '\n' );
        }
        
        return 0;
    }
    

    The program output is

    1.1 2.1 3.1 
    4.1 5.1 6.1 
    7.1 8.1 9.1 
    
    1.2 2.2 3.2 
    4.2 5.2 6.2 
    7.2 8.2 9.2 
    
    1.3 2.3 3.3 
    4.3 5.3 6.3 
    7.3 8.3 9.3 
    
    
    1.2 2.2 3.2 
    4.2 5.2 6.2 
    7.2 8.2 9.2 
    
    1.3 2.3 3.3 
    4.3 5.3 6.3 
    7.3 8.3 9.3 
    
    
    1.3 2.3 3.3 
    4.3 5.3 6.3 
    7.3 8.3 9.3