Jump to content
  • Advertisement

Archived

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

Gollum

NEWBIE - 2-dimensional array question.

This topic is 6590 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

Ok, I am a newbie. You''ve been warned. Let''s say I have a two-dimensional array, like so: int RoomExits[6][6]; How do I enter the values for one "row" of my array? I''ve tried something like this: RoomExits[1][] = {1,0,0,0,0,0}; But it doesn''t work. I can''t find syntax for this in any of my books. Thanks in advance, -gollumgollum

Share this post


Link to post
Share on other sites
Advertisement
Right, I know how to enter values for the whole array at once. What I can''t seem to find is how to enter (or change) values for a single row of the array at once, wihtout affecting the other rows.

- gollumgollum

Share this post


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

Right, I know how to enter values for the whole array at once. What I can't seem to find is how to enter (or change) values for a single row of the array at once, wihtout affecting the other rows.

- gollumgollum


You can only use the {...} when creating the variable you can't do it after creation. You are going to have to do it manually if you want to change an entire row at once. You could also use memcpy if you have an array of the new row.

    

int newRow[6] = {0,1,2,3,4,5}; // creating

memcpy( RoomExits[0], newRow, 6*sizeof(int) );

//change the row a little

newRow[3] = 7;
newRow[2] = 5;
// newRow is now [0,1,5,7,4,5]

memcpy( RoomExits[2], newRow, 6*sizeof(int) );





-----------
Andrew



Edited by - acraig on November 24, 2000 11:00:24 AM

Share this post


Link to post
Share on other sites
So, the reason I didn't see it explained anywhere is because ... you can't do it!

The memcpy thing isn't too bad, tho.

I am a little confused by the last parameter, the 6*sizeof(int). What does the "int" in the brackets refer to? I've never used memcpy, but the last param is how much to copy, right? Seems like you'd use sizeof(newRow[]) or something...

Thanks.

- gollumgollum



Edited by - Gollum on November 24, 2000 11:17:34 AM

Share this post


Link to post
Share on other sites
memcpy is not bad. It is fast enough for most programs, and if need more you can write your own. Alternate mem-mem copys include the double copy, mmx copy, 3d-now copy, sse copy..... no I don't know how they work. The only problem is that most memcpy's results are undefined if the two memory regions overlap.


Edited by - snowmoon on November 24, 2000 11:31:03 AM

Share this post


Link to post
Share on other sites
The first parameter of memcpy() is the memory location to start copying memory to. Likewise, the second parameter signifies where to start copying the memory from. And yes, the third parameter tells memcpy() how many bytes to copy. Since your array is of ints, you''re telling it to copy enough to cover 6 of them.

No, the computer isn''t smart enough to know the size of the array just by it''s name... if it did, we wouldn''t have all of those lovely crashes we''re so fond of...

Teej

Share this post


Link to post
Share on other sites
Don''t forget C is base 0. In other words, every array starts with 0, not 1!

your example code (which is not valid. =))

RoomExits[1][] = {1,0,0,0,0,0};

Would initialize the 2nd row, not the first! The first row is 0. (of course this code doesn''t work anyway, but just wanted to make the pt.)

Hope this helps.

Share this post


Link to post
Share on other sites
*grin*

I promise I did know about arrays indices starting at 0, but thanks for looking out for me!

So I guess that means that somewhere in the world there is someone who doesn''t know that, and they are more newbie than ME!

hehe.

- gollumgollum

Share this post


Link to post
Share on other sites
the behavior of memcpy''s is defined when the memory regions overlap. memcpy does a forward copy, so it works jim-dandy to do something like
  
memcpy(m_pvBuffer, (BYTE*)m_pvBuffer+dwStart, dwEnd - dwStart);
[/source]

you only get spurious results if you try to go the other way. Other copies are likely to work the same way, unless they were created to go backwards.

[source]
//This creates a dynamic 2D array

int** Rooms=0;
Rooms = new int*[down];
for(int j=0;j<down;j++)
Rooms[j] = new int[across];

//This replaces "a row of rooms"

//not exactly, but kinda how you wanted to

int* temp;
int* NewRowOfRooms = new int[across];
temp = Rooms[j];
Rooms[j] = NewRowOfRooms;
delete[] temp;

//This destroys a dynamic 2D array

for(j=0;j<down;j++)
delete[] Rooms[j];
delete[] Rooms;

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!