: Problem updating a two dimensional array
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!
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?
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?
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;
}
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;
}
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
i''m supposing that by iMap[j]=buffer[i+j]; you meant
iMap[j]=buffer[i+j]; and the forum is taking th as an italic
iMap[j]=buffer[i+j]; and the forum is taking th as an italic
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
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
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]
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]
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!
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!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement