0

I was trying to dig more into using arrays and pointers while I came across this problem:

main.cpp:13:7: error: cannot convert 'int [2][5][10]' to 'int*'

in assignment show=ary;

Here's the code:

#include <iostream>

using namespace std;

void arrayLearn(int *);
int main()
{
  int ary[2][5][10];
  int *show;
  ary[2][4][9]=263;
  ary[2][5][10]=100;
  show=ary;    //line 13
  arrayLearn(show);  //line 14
  cout <<"End process"<<endl;
  return 0;
}
void arrayLearn(int *arg)
{
    for(int i=0;i<100;i++)
    {
        cout<<"Pointer position: "<<i<<" Value: "<<*(arg++)<<endl;
    }

}

If I remove the line 13 and replace line 14 with the following code,

arrayLearn(ary[5][10]);

then the program compiles, but I don't understand why I should pass only two dimensions and not three. If I'm passing the pointer to the first item in the array then why can't I just pass the pointer like this?

arrayLearn(ary);

Please let me know if I've missed some vital concepts or failed to see a really simple mistake.

mins
  • 6,478
  • 12
  • 56
  • 75
Arpan Adhikari
  • 127
  • 1
  • 9
  • See http://stackoverflow.com/questions/29735567/c-incompatible-pointer-type-initializing/29735593#29735593. Different language, same problem. Your code looks like C anyway. – juanchopanza Apr 20 '15 at 05:23
  • don't bother using those language supported arrays. even the more because you declare them on the stack, you are going to cause stackoverflows. just use some nice matrix classes, gee. – v.oddou Apr 20 '15 at 05:23
  • See http://stackoverflow.com/questions/3911400/passing-2d-arrays to understand how to pass 2D arrays. Hopefully that will help you understand how to pass 2D arrays. – R Sahu Apr 20 '15 at 05:32
  • By the way: You are accessing your arrays out of bounds in line 11 and 12. Declaring an array with size 2 and accessing it with `ary[2]` is undefined behavior. The index to access the array is 0-based, that means that to access the second element you have to write `ary[1]`. – Excelcius Apr 20 '15 at 07:13

4 Answers4

1

If you are willing to use std::vector, which I highly recommend, you can use the following code to create a 2D vector:

using std::vector<int> Vec1;
using std::vector<Vec1> Vec2;
using std::vector<Vec2> Vec3;

Vec3 a(2, Vec2(5, Vec1(10, 0));

and then change the argument of arrayLearn to const Vec3& to call it using a.

void arrayLearn(const Vec3& arg) 
{
   // You'll need to use three loops to traverse all the elements.
   for ( auto const& v2 : arg )
   {
       for ( auto const& v1 : v2 )
       {
           for ( auto const& item : v1 )
           {
              // Use item
           }
       }
   }
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

As Abhishek stated, you are trying to pass a 3-dimensional array to a 1-dimensional object.

If the show variable is not used anywhere else, you can do the following:

#include <iostream>

using namespace std;

void arrayLearn(int *);
int main()
{
    int ary[2][5][10];

    // Code change explained below (see Note)
    ary[1][4][8] = 263;
    ary[1][4][9] = 100;

    // Pass the starting address of the array
    arrayLearn(&ary[0][0][0]);

    cout <<"End process"<<endl;
    return 0;
}

// Iterate through the array using the position of the elements in memory
void arrayLearn(int *arg)
{
    for(int i=0;i<=100;i++)
    {
        cout<<"Pointer position: "<<i<<" Value: "<<*(arg++)<<endl;
    }

}

Output:

...
Pointer position: 98 value: 263
Pointer position: 99 value: 100
End process

This way allows you to pass the starting address of the array to the function.

Note: It should be noted that your original array assignments ary[2][4][9] = 263; and ary[2][5][10] = 100; are outside the bounds of the array. Array indexes start at 0. So even though you have declared your array as ary[2][5][10]; To access the last array element, you would use ary[1][4][9];.

Community
  • 1
  • 1
SaoBiz
  • 1,249
  • 15
  • 16
0

In simple terms, ary is 3-Dimensional and show is 1-Dimensional. The compilation errors tell you that you can't convert 3D into 1D. Now, why does following work?

arrayLearn(ary[5][10]);

It is because, ary[5][10] is referring to 1D (a row or a column or a height, depends on how you visualize 3D array) and arrayLearn is also accepting 1D parameter.

if you want to pass ary using show then pass 2D like ary[5][10] to show variable, something like following :)

show = ary[5][10];
Abhishek
  • 6,912
  • 14
  • 59
  • 85
0

you can use STL data structurs like vector, map , linkedlist , ... that all of them are better that array both in emplementation and in performance.

but for solving exactly this problem, you must pass 3 dimention array to one function in this manner:

int array[10][7][8];

 void function_sample(int (*a)[7][8] , int length)
 {
     //for example for printing array cells
    for(int i =0 ; i< length ; i++)
    {
        for(int j =0 ; j< 7 ; j++)
        {
            for(int k =0 ; k< 8 ; k++)
            {
                cout << a[i][j][k] << endl
            }
        }
    }

    //....
 } 

and for calling this function :

int array[10][7][8] //= {initial your array};
function_sample(array , 10);
Mohsen Bahaloo
  • 257
  • 2
  • 2