• Announcements

Archived

This topic is now archived and is closed to further replies.

Sending two-dimensional arrays to a function

9 posts in this topic

You could use double poiters.
Like this : void foo(sometype **data);
0

Share on other sites
Oh, and...
the function has to know the dimentions of the array
0

Share on other sites
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...

0

Share on other sites
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.
0

Share on other sites
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?
0

Share on other sites

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[i] = new TILE[MAX_ROWS];}

code:
voidDestroy_TileMap() { INT i=0; if(tilemap != NULL) {    for (i=0; i < MAX_COLS; i++)       delete[] tilemap[i];    delete[] tilemap;    tilemap = NULL; }}

Here's an sample function to show how the whole 2d array is passed.
I don't pass my 2d array anyway, since it is globally defined.
But here is how I would do it:

code:
void Test_Func1(TILE_PTR_PTR tilemap) { INT x,y; if(tilemap == NULL)    return; // This method of tranversing a 2d array is wrong, btw, // but I do it anyway.  This is not linear, so it is not the  // fastest way to do it. for(INT y=0; y < MAX_ROWS; y++) {    for(INT x=0; x < MAX_COLS; x++) {       tilemap[x][y].width  = 32;       tilemap[x][y].height = 32;    } }}

Here's an sample function to show how one member of the 2d array is passed
from calling it like this:

Test_Func2( &(tilemap[0][0]) );

code:
void Test_Func2(TILE_PTR tile_ptr) { if(tile_ptr == NULL)    return; tile_ptr->width  = 32; tile_ptr->height = 32;}

If anything looks wrong, UBB might have eaten my code up.

Reaver

[This message has been edited by Reaver (edited October 29, 1999).]

0

Share on other sites
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[i] = 1;
}
}
with the call

DoStuffToMyArray(myArray,20*30);

It will go through the whole array using a single index 'i' from the base pointer 'array'

You also don't have to use &myArray[0][0]
because myArray is an equivilant pointer to that anyway.

[This message has been edited by MikeD (edited October 29, 1999).]

0

Share on other sites

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

0

Share on other sites
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
0

Share on other sites
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:
///////////////////////////////////

int dog[44][44];

{
for(int i;i<78;++i)
{
for(int j;j<78;++j)
{
grid[i][j]=1;
}
}
}

///////////////////////////////////

But how do I do the same thing with a function that can accept any size array?
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?

0