heya.. i wanted to ask if there was a way to give a pointer to a multidimensional array of, say, ints, as a function argument. i tried a somewhat natural approach, which seems to have failed.. i'm no expert at stuff like that, so i tried:

int numbers[ 10 ][ 10 ];
   inline void func( int **array ) {} // this doesn't matter really..
   int main( void ) {
      func( numbers );
      return 0;
   }

it looked ok to me... please tell me how to do it the right way

Edited 6 Years Ago by wellibedamned: n/a

>i tried a somewhat natural approach, which seems to have failed..
Completely understandable. However, the rule that an array name becomes a pointer to the first element only applies to the first dimension. So your function should be declared as one of the following:

// Pointer to an array of 10 int
inline void func( int (*array)[10] );
// Same thing but with array notation
inline void func( int array[][10] );

The former is what actually happens under the hood, and the latter is a convenient notation for it. Both are correct for passing a two dimensional array. Unfortunately, the size in both for the second dimension is required.

If you really want a pointer to a pointer (such as when both dimensions are unspecified), you can't use an array. You would need to simulate an array with dynamic memory.

Alternatively, you can avoid all of this crap by using the std::vector class. :)

1) How about using vectors ?
2) How about using 1d array to represent 2d ?
3) In passing 2D arrays, you need to specify the number of columns before hand, like so :

const int COL = 5;
void init(int Array[][COL], const int ROW){
//TODO logic here
}
int main(){
  const int ROW = 5;
  int Array[ROW][COL];
  init(Array,ROW);

  return 0;
}

OMG, I am going to kill my self, I shall call Narue, Double N from now on, i.e Ninja Narue because you always beat me to the post.

This article has been dead for over six months. Start a new discussion instead.