Function call

Write here if you have problems with your C++ source code

Moderator: Board moderators

Function call

Postby Artikali » Mon Oct 30, 2006 5:11 am

How can i built function which have multidimension array.
here is my code
Code: Select all
#include <iostream>
#include <cstdio>
using namespace std;
#define N 100
int put(int ** a){
   a[0][0]=1;
   return 0;
}
int main(){
   int c[N][N];
   put(c);
   cout<<c[0][0]<<endl;
   return 0;
}

it gave me compile error.
thanks
Artikali
Learning poster
 
Posts: 68
Joined: Wed Sep 21, 2005 5:27 pm

Postby Krzysztof Duleba » Mon Oct 30, 2006 5:24 am

Code: Select all
int put(int a[][100]) {
  // ...
}


In order to evaluate expressions like a[x][y], the compiler has to do something like *(a + x * 100 + y). Note that it must know all dimensions of the array, perhaps except for the first one. However, if you just pass is a pointer (int **), the compiler will have no clue about that.

You can also use vectors (of vectors), they work as expected.
For millions of years, mankind lived just like the animals. Then something happened which unleashed the power of our imagination. We learned to talk and we learned to listen...
User avatar
Krzysztof Duleba
Guru
 
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland


Return to C++

Who is online

Users browsing this forum: No registered users and 0 guests