Multidimensional array of pointers to a function

Hello. I want to use my multidimensional array of pointers on a function, but i don't know how to pass this array to the function.
I want this code to work, but no way to do it :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
using namespace std;

void function (int *Array[][]);

int main()
{
    int *array[2][2];
    for (int i=0; i<3; i++){
        for (int j=0; j<3; j++){
            *array [i][j]=0;
        }
    }
    function (&array);
    for (int i=0; i<3; i++){
        for (int j=0; j<3; j++){
           cout << *array [i][j]=0;
        }
    } 
}
void function (int *Array[][]){
    for (int i=0; i<3;i++){
        for (int j=0; j<3; j++){
            *Array [i][j]=1;
        }
    }
}

Some compile errors:
6: declaration of 'Array' as multidimensional array must have bounds for all dimensions except the first
19: no match for 'operator=' (operand types are 'std::basic_ostream<char>' and 'int')
23: same as 6
26:'Array' was not declared in this scope
I'm not sure exactly what your program is intended to do, as it contains a number of errors. When passing an array to a function, all dimensions but the first must be explicitly stated (or state all of them). In the for loops, you are accessing elements 0, 1 and 2 (three elements, but the array has only two elements. That's an out of bounds error. A pointer needs to point to something. I'm not sure your code does that.

So here's an example which at least compiles and does something.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
using namespace std;

void function (int *Array[][2]);

int main()
{
    int data[4] = {2, 3, 5, 7};
    
    int *array[2][2];
    int *n = data;
    
    // make sure the pointers point to an integer
    for (int i=0; i<2; i++) {
        for (int j=0; j<2; j++) {
            array [i][j] = n++;    
        }
    }
    
    for (int i=0; i<4; i++)
        cout << data[i] << ' ';
    cout << endl;
    
    function (array);
    
    for (int i=0; i<4; i++)
        cout << data[i] << ' ';
    cout << endl;
    
    return 0;    
    
}

void function (int *Array[][2]) {
    
    int dat2[4] = {19, 17, 13, 11};
    int *p = dat2;
  
    // leave the pointers alone, and alter the value which it is pointing to.  
    for (int i=0; i<2;i++) {
        for (int j=0; j<2; j++) {
            *Array[i][j] = *p++; 
        }
    }
}


Output:
2 3 5 7
19 17 13 11


I have a question.

Array[][2] means that we have an array with two rows and columns? why is the first brackets or whatever they're called ( I mean the '[]' ) are empty?

Is that some kind of a rule?
Last edited on
As I said above,
chervil wrote:
When passing an array to a function, all dimensions but the first must be explicitly stated (or state all of them).


The actual array is declared as int *array[2][2]; that is, all dimensions must be stated.

When a function is called, the first dimension may be omitted.

See the tutorial for a fuller explanation: http://www.cplusplus.com/doc/tutorial/arrays/


Last edited on
Topic archived. No new replies allowed.