• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

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

CoolMike

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 this post


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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


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

0

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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:
///////////////////////////////////
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?

0

Share this post


Link to post
Share on other sites