• Advertisement

Archived

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

: Problem updating a two dimensional array

This topic is 5844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


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

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?

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;
}

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

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

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

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]

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!

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.

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

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

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.

Share this post


Link to post
Share on other sites

  • Advertisement