Sending two-dimensional arrays to a function

Started by
8 comments, last by CoolMike 24 years, 5 months ago
You could use double poiters.
Like this : void foo(sometype **data);
Advertisement
Oh, and...
the function has to know the dimentions of the array
Hey,

if you want to send a regular multi-dimensional array to a function, in the function prototype/declaration, you have to state the size for every dimension except the first.

for example

say you have this array:

// ...
int array[10][20];
// ...

to have a function take such an array, you would declare it like this

int Function( int data[][20] )
{
// ... whatever
}

i hope that helps some...


You must either specify the minor dimension of the array or just send a single pointer to the data with sizes. There is no double indirection for 2D arrays, it's just an implicit pointer increment based on the known minor dimension, the major dimension is irrelevant except for overflow.
I know that you only have to specify the minor dimension, but is it possible to have a function that can receive ANY size array? What if I have another size array with a different size minor dimension? Can I still send it to the function?

Here is a simple way to dynamically use a multi-dimensional 2D array.

code:
// defines#define	MAX_COLS	128#define	MAX_ROWS	128// structstypedef struct _TILE { INT width; INt height;} TILE, *TILE_PTR, **TILE_PTR_PTR;// globalsTILE_PTR_PTR		tilemap;

code:
// functionsvoidCreate_TileMap() { INT i=0; tilemap = new TILE_PTR[MAX_COLS]; for (i=0; i < MAX_COLS; i++)    tilemap = new TILE[MAX_ROWS];<P>}<BR></pre><HR></BLOCKQUOTE><P><BLOCKQUOTE><font size="1" face="Verdana, Arial">code:</font><HR><pre><BR>void<BR>Destroy_TileMap() {<P> INT i=0;<P> if(tilemap != NULL) {<BR>    for (i=0; i < MAX_COLS; i++)<BR>       delete[] tilemap;<BR>    delete[] tilemap;<P>    tilemap = NULL;<BR> }<BR>}<BR></pre><HR></BLOCKQUOTE><P><BR>Here's an sample function to show how the whole 2d array is passed.<BR>I don't pass my 2d array anyway, since it is globally defined.<BR>But here is how I would do it:<P><BR><BLOCKQUOTE><font size="1" face="Verdana, Arial">code:</font><HR><pre><BR>void <BR>Test_Func1(TILE_PTR_PTR tilemap) {<P> INT x,y;<P> if(tilemap == NULL)<BR>    return;<P> // This method of tranversing a 2d array is wrong, btw,<BR> // but I do it anyway.  This is not linear, so it is not the <BR> // fastest way to do it.<P> for(INT y=0; y < MAX_ROWS; y++) {<BR>    for(INT x=0; x < MAX_COLS; x++) {<P>       tilemap[x][y].width  = 32;<BR>       tilemap[x][y].height = 32;<P>    }<BR> }<P>}<BR></pre><HR></BLOCKQUOTE><P> <BR>Here's an sample function to show how one member of the 2d array is passed<BR>from calling it like this:<P>   Test_Func2( &(tilemap[0][0]) );<P><BLOCKQUOTE><font size="1" face="Verdana, Arial">code:</font><HR><pre><BR>void <BR>Test_Func2(TILE_PTR tile_ptr) {<P> if(tile_ptr == NULL)<BR>    return;<P> tile_ptr->width  = 32;<BR> tile_ptr->height = 32;<BR>}<BR></pre><HR></BLOCKQUOTE><P><BR> If anything looks wrong, UBB might have eaten my code up.<P>Reaver<p>[This message has been edited by Reaver (edited October 29, 1999).]

Okay, surely an array is merely a pointer to a piece of memory with some nice indexing ability so that we can access it with ease.

In reality
int myArray[20][30];
Just does
myArray = malloc(sizeof(int) * 20 * 30);
And calling
myArray[13][14]
returns a pointer
(int*) myArray + (13*30) + 14;
Knowing this, when you pass myArray into a function
DoStuffToMyArray(int* array,int numObjects)
{
int i;
for(i=0;i < numObjects;i++)
{
array = 1;<BR> }<BR>}<BR>with the call<P>DoStuffToMyArray(myArray,20*30);<P>It will go through the whole array using a single index 'i' from the base pointer 'array'<P>You also don't have to use &myArray[0][0]<BR>because myArray is an equivilant pointer to that anyway.<BR><p>[This message has been edited by MikeD (edited October 29, 1999).]


I just want to add that I use different methods to access my
my 2D array, and passing &(tilemap[31][23]) for example as a
function's parameter is easy for me because I just want to work
on this particular tilemap member. Usually you don't see this
syntax anywhere, I just have it stored away in a pointer variable
when I need this data.

I would also recommend that anyone who doesn't know how big
their array data will eventually become, to dynamically allocate
their arrays because it will save you a lot of unnecessary headaches
when you run out of stack memory space and your program starts crashing.

I also like to keep them global, so you don't have to pass them
as parameters, but that's my preference.

And the reason why I have my 2d array like tilemap[COLS][ROWS] or
tilemap[x][y] is that my tiles start at the upper left corner, and go
left to right across the screen, then down. This is how I visualize
my tiles and why I don't traverse them linearly.

Reaver

I want to be able to send a two-dimensional array to a function (of any size), but I can't figure it out.

This code is possible:
///////////////////////////////////
void readGrid(grid[44][44]);

int dog[44][44];

void readGrid(grid[44][44])
{
for(int i;i<78;++i)
{
for(int j;j<78;++j)
{
grid[j]=1;<BR> }<BR> }<BR>}<P>readGrid(dog);<P>///////////////////////////////////<P>But how do I do the same thing with a function that can accept any size array?<BR>For instance, if I have a different array, maybe called fox[43][22], would it be possible for me to send that array to the exact same function? If so, how?

Just in case my reply was mis-interpreted, I wasn't questioning Reavers methods, just posting up my own.
After all, it would be so boring if we all coded the same way

This topic is closed to new replies.

Advertisement