0

I'm facing some difficulties with pointers for 3D arrays in C++. I've a array of Q[3][N][N] which I want to pass to a function to print the values at [0][i][j] location (and also for [1][i][j] and [2][i][j]). How can I achieve this? Will it be more convenient to use Q[i][j][0] etc?

for 2D, the following piece of code works just fine when I give &Q[0][0] to the *var:

template <typename T>
void print2d(T *var, int I, int J){
cout << endl;
for (int j = 0; j < J; j++){
    for (int i = 0; i < I; i++){
        cout << setprecision(3) << setw(12) << *(var + (N*i + j));
    }
    cout << endl;
}
cout << endl;
}

I'm using the same approach to write a similar function for 3D which does not write out the correct values: Can anybody let me know the correct way to point to the correct address of Q[i][j][1]. In input argument, I'm giving the address of Q[0][0][0]. Should I use different addresses (such as Q[i][j][1]) if I want to write out for that particular value of k?

template <typename T>
void print3d(T *var, int I, int J, int K){
    cout << endl;
        for (int j = 0; j < J; j++){
            for (int i = 0; i < I; i++){
                cout << setprecision(3) << setw(12) << *(var + I*J*i + I*j + K);
            }
            cout << endl;
        }
    cout << endl;
}
Pranav
  • 560
  • 1
  • 8
  • 21

2 Answers2

0

Sample example, N must be const. But consider using vector.

#include <stdio.h>
#include <iostream>
using namespace std;
const int N = 5;

void func2(int* tab, int A, int B, int C)
{
    printf("%d\n", tab[N*N*A + N*B + C] );
}

void func(int tab[3][N][N])
{
    for (int i = 0; i < 3; ++i)
    for (int j = 0; j < N; ++j, printf("\n"))
    for (int k = 0; k < N; ++k)
    {
        printf("%d ", tab[i][j][k]);
    }
}
int main()
{
    int tab[3][N][N];
    int p = 0;
    for (int i = 0; i < 3; ++i)
    for (int j = 0; j < N; ++j)
    for (int k = 0; k < N; ++k)
    {
        tab[i][j][k] = p++;
    }
    func(tab);
    printf("\n");
    func2((int*)tab, 1, 1, 3);
}
AdamF
  • 2,501
  • 17
  • 30
  • Can func be in different file? And I guess you are passing an entire array, right? How I can I achieve this with pointers? – Pranav Apr 09 '14 at 22:27
  • I updated example. The func2 is not so beautifoul and it assume than tab is in continuous memory block. (in pointers, where arrays are allocated dynamicly it is not true) – AdamF Apr 09 '14 at 22:47
  • In this example, you can use template parameters for the array dimensions of `func`, instead of `N`, so that you can support arrays of different sizes. – M.M Apr 10 '14 at 03:32
0

I agree with Adam's answer but cant comment, need more points there.

1) Arrays are not pointers. Multidimensional arrays cannot be passed via a pointer, needs to have all but one dimension constant. Here is an answer to help understand.

Cannot cast array to pointer

How do I pass a reference to a two-dimensional array to a function?

http://www.cplusplus.com/forum/articles/17108/

You might want to try vectors from STL instead.

2) Templates are compiled only when there is an instance of it used, that is why its declaration and usage is generally put in one file. Template Compilation

Community
  • 1
  • 1
  • I agree that vectors maybe convenient to use compared to arrays however my program will require lots of calculations (similar program in matlab takes ~6 hrs). I read that convenience of vectors comes at a price of performance, which I want to avoid. Kindly let me know if I am wrong. – Pranav Apr 09 '14 at 23:02
  • A vector of vectors doesn't use contiguous storage, so it's probably not suitable as a replacement for a multi-D array. However, it is less syntactical drama to use a 1-D array anyway, and calculate offsets into it to find the elements you want. – M.M Apr 10 '14 at 03:31