Jump to content

  • Log In with Google      Sign In   
  • Create Account

Sending two-dimensional arrays to a function


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 CoolMike   Members   -  Reputation: 122

Like
Likes
Like

Posted 31 October 1999 - 11:22 PM

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[i][j]=1;
}
}
}

readGrid(dog);

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

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?


Sponsor:

#2 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 28 October 1999 - 01:25 PM

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

#3 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 28 October 1999 - 01:28 PM

Oh, and...
the function has to know the dimentions of the array

#4 zerovoid   Members   -  Reputation: 122

Like
Likes
Like

Posted 28 October 1999 - 01:44 PM

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...




#5 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 28 October 1999 - 01:58 PM

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.


#6 CoolMike   Members   -  Reputation: 122

Like
Likes
Like

Posted 29 October 1999 - 03:56 AM

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?

#7 Reaver   Members   -  Reputation: 122

Like
Likes
Like

Posted 29 October 1999 - 04:04 AM


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

code:

// defines
#define MAX_COLS 128
#define MAX_ROWS 128


// structs
typedef struct _TILE {

INT width;
INt height;

} TILE, *TILE_PTR, **TILE_PTR_PTR;


// globals
TILE_PTR_PTR tilemap;


code:

// functions
void
Create_TileMap() {

INT i=0;

tilemap = new TILE_PTR[MAX_COLS];
for (i=0; i < MAX_COLS; i++)
tilemap[i] = new TILE[MAX_ROWS];

}


code:

void
Destroy_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).]


#8 MikeD   Members   -  Reputation: 158

Like
Likes
Like

Posted 29 October 1999 - 04:29 AM

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).]


#9 Reaver   Members   -  Reputation: 122

Like
Likes
Like

Posted 29 October 1999 - 12:06 PM


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


#10 MikeD   Members   -  Reputation: 158

Like
Likes
Like

Posted 31 October 1999 - 11:22 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS