passing 2 dim array
-
How can you reuse a function in which you want to pass a 2 dimentional array using different sizes? const int arr_size = 20; main { int 2_array[arr_size][arr_size]; .... print_arr(2_array); } //parameter must be passed like this void print_arr(int temp[][arr_size]) { ..... } Kevin Shaffer kshaff03@msn.com
-
How can you reuse a function in which you want to pass a 2 dimentional array using different sizes? const int arr_size = 20; main { int 2_array[arr_size][arr_size]; .... print_arr(2_array); } //parameter must be passed like this void print_arr(int temp[][arr_size]) { ..... } Kevin Shaffer kshaff03@msn.com
const int arr_size = 20;
main
{
int 2_array[arr_size][arr_size];
....
print_arr(&2_array);}
//parameter must be passed like this
void print_arr(int *temp)
{
.....
}Don't try it, just do it! ;-)
-
const int arr_size = 20;
main
{
int 2_array[arr_size][arr_size];
....
print_arr(&2_array);}
//parameter must be passed like this
void print_arr(int *temp)
{
.....
}Don't try it, just do it! ;-)
When I try that, I get the following compilation error: function call print_arr'(int (*)[10][10])' does not match 'print_arr(int*)' error occurs when I call the function: print_arr(&2_array); //<------ Could it be my compiler? I have to use Metrowerks Codewarrior on this particular computer. Kevin Shaffer kshaff03@msn.com
-
When I try that, I get the following compilation error: function call print_arr'(int (*)[10][10])' does not match 'print_arr(int*)' error occurs when I call the function: print_arr(&2_array); //<------ Could it be my compiler? I have to use Metrowerks Codewarrior on this particular computer. Kevin Shaffer kshaff03@msn.com
kshaff03 wrote: print_arr(&2_array); //<------ change it to printf_arr((int*)&2_array); My God is more powerfull Than Your God.
-
How can you reuse a function in which you want to pass a 2 dimentional array using different sizes? const int arr_size = 20; main { int 2_array[arr_size][arr_size]; .... print_arr(2_array); } //parameter must be passed like this void print_arr(int temp[][arr_size]) { ..... } Kevin Shaffer kshaff03@msn.com
My corrections in bold
**int** main**()** { // int 2_array[arr_size][arr_size]; // Illegal variable name. **int array_2[arr_size][arr_size];** print_arr(2_array); **return 0;** }
Here's a small example program showing 2 diffrent versions of passing 2-dim arrays#include < iostream > using namespace std; const int arr_size = 5; // Should pass the upper limit to this function since it // cannot know how many elements there are in each dimension void print_arr(int *temp) { for (int i = 0;i < arr_size;++i) { cout << endl; for (int j = 0;j < arr_size; ++j) cout << temp[i*arr_size + j] << " "; } cout << endl; } // Same here for the number of rows void print_arr2(int temp[][arr_size]) { for (int i = 0;i < arr_size; ++i) { cout << endl; for (int j = 0;j < arr_size;++j) cout << temp[i][j] << " "; } cout << endl; } int main(int argc, char* argv[]) { int array2d[arr_size][arr_size] = {0}; print_arr(&array2d[0][0]); // get address of first element. No need for casts print_arr2(array2d); return 0; }
HTH Jonas “Our solar system is Jupiter and a bunch of junk” - Charley Lineweaver 2002