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

ghost1206

: Problem updating a two dimensional array

13 posts in this topic

This must be a very common and easy-to-solve problem, but I cannot find the right way. My problem is that I''ve created a function that fills a two dimensional array with data from a file. The problem is that the array seems not to be updated (the behavior of the program shows that) so I gather that I have passed the array by value (i.e. the function used a copy of the array and not the array itself) and not by reference. Here is the signature of the function: int data[100][50]; ... DDLoadMapData(int iMap[][50]){ ... So how do I pass a two dimensional array by reference? I read somewhere that a two dimensional array is always passed by reference but I am not sure if it''s correct. Please, help!
0

Share this post


Link to post
Share on other sites
That should work, the problem is in your code, can you show the code of the function?
0

Share this post


Link to post
Share on other sites
Sure, here it is:

extern "C" HRESULT DDLoadMapData(int *iMap[][60])
{
int fh;
BYTE buffer[8640];

fh = _lopen("map.dat", OF_READ);
if (fh != -1)
{
_lread(fh, buffer, sizeof(buffer));
_lclose(fh);
return DD_OK;
}

for(int i=0; i<60; i++)
for(int j=0; j<144; j++)
*iMap[j]=buffer[i+j];

// for testing purposes.
*iMap[2][3]=3;

return -1;
}

I forgot to mention that I call this with: "DDLoadMapData(map);" in case it is of any importance. It does compile fine but the array "map" is not updated with the values from the file. So I assume the iMap is a copy of map and not map itself. Or not?

0

Share this post


Link to post
Share on other sites
Oops! Ignore the prvious one. This is what I tried:

extern "C" HRESULT DDLoadMapData(int iMap[][60])
{
int fh;
BYTE buffer[8640];

fh = _lopen("map.dat", OF_READ);
if (fh != -1)
{
_lread(fh, buffer, sizeof(buffer));
_lclose(fh);
return DD_OK;
}

for(int i=0; i<60; i++)
for(int j=0; j<144; j++)
iMap[j]=buffer[i+j];

return -1;
}
0

Share this post


Link to post
Share on other sites
quote:

iMap[j]=buffer[i+j];


is this a typo?
it should be iMap[j]=buffer[i+j];

ok, I saw what happened it is not a typo
quote:

return DD_OK;


your function is exiting there , you must return after iMap[j]=buffer[i+j];
or do it inside th parenthesis before the return

Edited by - hewhay on January 18, 2002 1:54:12 PM
0

Share this post


Link to post
Share on other sites
i''m supposing that by iMap[j]=buffer[i+j]; you meant
iMap[ i][j]=buffer[i+j]; and the forum is taking th [ i] as an italic
0

Share this post


Link to post
Share on other sites
Ya know I don't really know much about C which is what that looks like, but there are several things here I've never seen before:
int iMap[][50]
I have absolutly no clue what that is doing. I've never seen an empty array variable... Maybe thats normal in C I don't know. I guess thats just something about C i don't know, but sounds like something I might find useful if anyone wants to explain.

Edited by - Xanthen on January 18, 2002 2:03:21 PM
0

Share this post


Link to post
Share on other sites
It just means that you know the second dimension of the array but not the first. It can only be done to the first dimension

so
int test (int arr[][50])

will treat an array like: int arr[2][50] like int arr[34][50] but it won''t accept arr[1][23]
0

Share this post


Link to post
Share on other sites
Damn! Thanks hewhay! That was a very stupid mistake, sorry to bother you with that. There are times when I feel a real idiot.

I''ll try to redeem myself by explaining the iMap[][60] part. It''s a actually a pointer to a 60 int array. Never forget that an array is always a pointer for the compiler. So to represent a two dimensional array the compiler only needs a pointer to an array with a steady dimension (60 in our case). Thus, You can write iMap[][60] or (*iMap)[60] instead.

I still find the book writen for C from the Waite Group to be an excellent ref.

What I am not sure about, is how the compiler will treat the pointer. Will it use to make a copy of the array for use only in the function. Or will it simply generate a second pointer pointing to the same array. It must be the second one, since I am not noticing any severe overhead to the app.

As you can notice from my rediculous mistake, Lugie, yes I am a beginner!
0

Share this post


Link to post
Share on other sites
Found the bug!

ok guys! We certainly missed this one! After I eliminated the return sentence I still couldn''t get the program to display the map in the correct way. After a little nap, though, I got it: the right way to pass the data from the sequential array to the two dimensional one is the following:

for(int j=0; j<60; j++)
for(int i=0; i<144; i++)
iMap[j]=buffer[i+(j*144)];

Yes, I know! Rediculous error. But what the hell. I''ve accepted the fact that I am no genious long time ago! Thanks for your time to reply, anyway.
0

Share this post


Link to post
Share on other sites
Is it just me, or will this not work.
for(int j=0; j<60; j++) // it counts to 59
for(int i=0; i<144; i++) // it counts to 143
iMap[j]=buffer[i+(j*144)]; // assigns 143 + 59*144 to iMap[59]

Shouldn't it be:

for(int j=0; j<60; j++)
{
for(int i=0; i<144; i++)
iMap[j]=buffer[i+(j*144)];
}

Wouldn't this be the way to update each element?

Edited by - grandmlee on January 22, 2002 6:58:27 AM
0

Share this post


Link to post
Share on other sites
quote:
Original post by GrandMLee

  
Is it just me, or will this not work.
for(int j=0; j<60; j++) // it counts to 59

for(int i=0; i<144; i++) // it counts to 143

iMap[i][j]=buffer[i+(j*144)]; // assigns 143 + 59*144 to iMap[59]



Shouldn''t it be:

  
for(int j=0; j<60; j++)
{
for(int i=0; i<144; i++)
iMap[i][j]=buffer[i+(j*144)];
}


Wouldn''t this be the way to update each element?


No, both of them are identical


This
  
for(int x=0; x<3; x++)
for(int y=0; y<3; y++)
for(int z=0; z<3; z++)
function(x,y,z);

is the same as this
  
for(int x=0; x<3; x++)
{
for(int y=0; y<3; y++)
{
for(int z=0; z<3; z++)
{
function(x,y,z);
}
}
}


This is because a for statement treats the next line as a set of parentheses. This is the same as a while


you can do this for example
for(int x=0; x<3; x++);
or this
for(int x=0; x<3; x++){}
and they will both just make x count to 3.




Beer - the love catalyst
good ol'' homepage
0

Share this post


Link to post
Share on other sites
yeah, I realized that as I was almost drifting off to sleep. I must have been tired yesterday or something.
0

Share this post


Link to post
Share on other sites