2-D sub array

Mar 15, 2009 at 12:48pm
How to pass a sub 2-D array to a function. For e.g. given the array below
1
2
3
1  2  3  4
5  6  7  8
9 10 11 12


In a function F. I want to access the bottom right of the array just
1
2
 7  8
11 12


So how to do that? Can someone help me out in this?
Mar 15, 2009 at 1:11pm
Considering array's as constant pointers, you can simply pass the array and the sizes:

1
2
3
4
5
6
7
8
9
10
11
12
13
//function:
void function(int** array, int Xsize, int Ysize);

...

//declare sizes:
const int Xsize = 5;
const int Ysize = 6;
//the array:
int myArray[Xsize][Ysize];

//now we can simply pass the array to the function:
function(myArray,Xsize,Ysize);


Now we have our array in the function (notice that, because arrays are constant pointers, chances in the elements inside the function will be the same for the elements of the original array). It's now simple to access for example the botton right:

1
2
3
4
5
6
7
for (int x=Xsize-2;x<Xsize;x++)
{
    for (int y=Ysize-2;y<Ysize;y++)
    {
    
    }
}
Last edited on Mar 15, 2009 at 1:11pm
Mar 15, 2009 at 4:36pm
I want to access them as a[0][0], a[0][1] and so on. So, in the case above, I should get a[0][0] = 7, a[0][1]= 8, a[1][0]=11 and a[1][1] = 12.
Mar 15, 2009 at 4:54pm
You can use the for-loop is posted to store the value's into another array, but it isn't really necessary. array[x+somevalue]; isn't much harder to understand as array[x];, IMO.
Mar 15, 2009 at 5:14pm
Ugh @ multidimensional arrays. I don't know why everybody likes them so much. Syntax hell.

Re Scipio:
1
2
3
4
void function(int** array, int Xsize, int Ysize);

int myArray[Xsize][Ysize];
function(myArray,Xsize,Ysize);


This wouldn't work. myArray is not a two stage pointer, it's a 2D array, which is stored the same as a 1D array, just with syntax differences (so it's a one stage pointer, not 2)

The way to do this would be:

1
2
3
void function(int array[][6])
{
}


Notice that the size of the lowest element(s) must be known and constant. Only the first pair of brakets can remain empty. You cannot pass the sizes to the function as Scipio suggested (well I suppose you could but it wouldn't make much sense for the lower brakets). That is only possible when the array is created dynamically (in which case it is a two stage pointer, and Scipio's example would be correct).

Or... you could template the function to make the sizes more dynamic. However that is a bit more complicated and seems like overkill for this.

EDIT:

Doh I totally missed the whole "sub array" thing. XD.

Something similar to what Scipio suggested is probably the best way to go, here. Due to the way these arrays are stored, you can't just extract an arbitrary group of them with a single pointer. Either work with the array in full, or make a class to abstract this behavior.
Last edited on Mar 15, 2009 at 5:25pm
Mar 15, 2009 at 5:38pm
Notice that the size of the lowest element(s) must be known and constant

You can use the template trick:
1
2
template<unsigned size>
void function( int array[][size] )


for the sub array, you can create a new array in the function and setting its elements to the corresponding of the passed array
eg:
yourNewArray[0][0] = yourArgumentArray[1][2];
Topic archived. No new replies allowed.