0

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
HotSauce
  • 23
  • 4
  • First, it's not a 3D array, it's an array of arrays of arrays. That matters. Thus `tmpMatrix[i]` will give an array of arrays. C- style casting to a double pointer is not correct. You should use the proper array type, or even `std::array` preferably. – JHBonarius May 15 '21 at 10:52
  • Does this answer your question? [Why can't I treat an array like a pointer in C?](https://stackoverflow.com/questions/12676402/why-cant-i-treat-an-array-like-a-pointer-in-c) – JHBonarius May 15 '21 at 10:53
  • Hi, thank you for the feedback. When you say proper array type, could you please elaborate or provide any links to demonstrate? – HotSauce May 15 '21 at 10:55

2 Answers2

2

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 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

void test(int msize, double * m), here m is a pointer to double(acts kind of array while using), doesnt represent a 2D array. So you can do access the 2D array elements by explicitly doing address manipulation to traverse the elements or you can change the function to recieve a 2D array as a pointer to Array double (*m)[Z] and you can traverse the 2D array as a series of 1D arrays. You can refer answer at Create a pointer to two-dimensional array and what is `int *userMask[3][4]` pointing to?

LearningC
  • 3,182
  • 1
  • 12
  • 19