Passing multidimensional arrays by reference

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
#include <iostream>

using namespace std;

int *ReverseArray(int *orig, int b)
{
int swap;
int c = -1;
for(int a=-4; a < -2; a++)
{
swap = orig[a];
orig[a]=orig[c];
orig[c]=swap;
c--;
}
return orig;
}

int main()
{
const unsigned short int SIZE = 2;
int ARRAY[SIZE][SIZE] = {{1,2},{3,4}};
int *arr = ARRAY[SIZE];

for(int i = 0; i < 2 * SIZE; i++)
cout << arr[i-2*SIZE] << " ";

cout << std::endl << endl;
arr = ReverseArray(arr, SIZE);
cout << endl;
for(int i = 0; i < 2 * SIZE; i++)
std::cout << arr[i- 2 * SIZE] << " ";

cout << std::endl;

return 0;
}


I managed to cobble this program together, it works as intended, however in a certan segment:

1
2
3
4
5
6
const unsigned short int SIZE = 2;
    int ARRAY[SIZE][SIZE] = {{1,2},{3,4}};
    int *arr = ARRAY[SIZE];
    
    for(int i = 0; i < 2 * SIZE; i++)
        cout << arr[i-2*SIZE] << " ";


by messing around with it I found the only way I could get it to work was to offset arr[] by the number of items in my multidimensional array, in this case four. The program compiles, but I don't want to go through the rest of my life offsetting all my multidimensioanl array pointers by the size of the array. Could someone post the correct syntax for pointing to/passing a multidimensional array by reference? Thanks.
Topic archived. No new replies allowed.