0

I am looking for a way to access a multidimensional Array that is passed via pointer. I followed the question here (Create a pointer to two-dimensional array) to create my array and a pointer to it. I could even pass the pointer to the function, but I can't use the pointer to access the array values. What's the correct way to access the values inside the function.

main.cpp:

MyArrayGenerator theArrGen = MyArrayGenerator();
const int size = 9;
int generatorArray[size][size][10];

theArrGen.generateArray(size, generatorArray[size][size+1]);

The method in my class:

void MyArrayGenerator::generateArray(int size,int* pointerToMultiDimArr)
{
    int height = size + 1;
    // this ptr points to the array address
    int* ptr = pointerToMultiDimArr;
    // not working:
    ptr[1][1][1] = 123;
}

this throws the compiler error https://msdn.microsoft.com/de-de/library/fhexbxk9.aspx wich means that the pointer is not declared as array.

I guess my method argument needs to change since it wants a pointer and does not know that it will be an array. My question is: How should the method argument look like and how do I access the array in my method after that. Later on I want the user to input the size of the multidimensional array and

    const int size = 9;

is just a placeholder

Community
  • 1
  • 1
TheCell
  • 59
  • 2
  • 10
  • Why have you tagged this as *C*? – Ed Heal Mar 16 '16 at 22:23
  • sorry thought this problem would be interchangeable between the two. Deleted the C tag. – TheCell Mar 16 '16 at 22:25
  • The language with classes is C++, not C. And there is no multidimensional array. A pointer is not even a 1D array. – too honest for this site Mar 16 '16 at 22:27
  • When passing a pointer to a multi-dimensional array to a function, the receiving function only gets a pointer to the first element. All information about the dimensions is lost. You will have to calculate the index yourself or use the correct syntax for passing an array. – Thomas Matthews Mar 16 '16 at 23:11

1 Answers1

0

Use pointer notation to access elements! Here is an example to access and use 2d array's pointer this program prints transpose of a matrix

#include<iostream>
int **arr_input(int row, int col)
{
    int **arr;
    arr = new int*[row];
    for (int i = 0; i < row; i++)        //array size
    {
        *(arr + i) = new int[col];
    }
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            cin >> *(*(arr + i) + j);
        }
    }   //input array
    return arr;
}


void transpose(int**arr, int row, int col)
{
    for (size_t i = 0; i < row; i++)
    {
        for (size_t j = 0; j < col; j++)
        {
            cout << *(*(arr + j) + i) << "  ";      //pointer notation//
        }   
        cout << endl;
    }
}
void main(){
          int row ,col,**ptr1;
        cout << "Enter size of Square Matrix: ";    cin >> row; col = row;
        cout << "\n\tEnter the elements of Matrix\n";
        ptr1 = arr_input(row, col);    //calling array input function
        cout << "\nAdress of pointer is " << ptr1<<endl;
        transpose(ptr1, row, col);  //calling transpose function
        system("pause");
   }
  • The problem is that the receiving function has no idea on the capacities of the dimensions. The evaluation of `arr[1][2][1]` is meaningless without the capacities of the dimensions. The second dimension may have 5 elements, it may have 512. – Thomas Matthews Mar 16 '16 at 23:13
  • thanks for actually trying to answer and not just blaming :). Does this mean If I want to have a multidimensional array I need a pointer to a pointer wich then points to the array. I will also need a for loop to create the multi dimensional array (the arr_input method)? Or is it possible to create the array via `int myArr[3][3][3];`. – TheCell Mar 17 '16 at 08:47