Jump to content

  • Log In with Google      Sign In   
  • Create Account


: Problem updating a two dimensional array


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
13 replies to this topic

#1 ghost1206   Members   -  Reputation: 122

Like
Likes
Like

Posted 18 January 2002 - 03:27 AM

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!

Sponsor:

#2 hewhay   Members   -  Reputation: 122

Like
Likes
Like

Posted 18 January 2002 - 04:58 AM

That should work, the problem is in your code, can you show the code of the function?

#3 ghost1206   Members   -  Reputation: 122

Like
Likes
Like

Posted 18 January 2002 - 05:52 AM

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?



#4 ghost1206   Members   -  Reputation: 122

Like
Likes
Like

Posted 18 January 2002 - 05:57 AM

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


#5 Lugie   Members   -  Reputation: 122

Like
Likes
Like

Posted 18 January 2002 - 06:04 AM

and you''re a beginner?

#6 hewhay   Members   -  Reputation: 122

Like
Likes
Like

Posted 18 January 2002 - 06:49 AM

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

#7 hewhay   Members   -  Reputation: 122

Like
Likes
Like

Posted 18 January 2002 - 06:56 AM

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

#8 Xanthen   Members   -  Reputation: 115

Like
Likes
Like

Posted 18 January 2002 - 07:01 AM

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

#9 hewhay   Members   -  Reputation: 122

Like
Likes
Like

Posted 18 January 2002 - 07:12 AM

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]

#10 ghost1206   Members   -  Reputation: 122

Like
Likes
Like

Posted 18 January 2002 - 08:43 AM

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!

#11 ghost1206   Members   -  Reputation: 122

Like
Likes
Like

Posted 21 January 2002 - 06:51 PM

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.


#12 GrandMLee   Members   -  Reputation: 122

Like
Likes
Like

Posted 21 January 2002 - 11:57 PM

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

#13 Dredge-Master   Members   -  Reputation: 175

Like
Likes
Like

Posted 22 January 2002 - 12:23 AM

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

#14 GrandMLee   Members   -  Reputation: 122

Like
Likes
Like

Posted 22 January 2002 - 09:32 PM

yeah, I realized that as I was almost drifting off to sleep. I must have been tired yesterday or something.




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