#### Archived

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

# NEWBIE - 2-dimensional array question.

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

## 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 on other sites
Like this...

int exists[6][3] =
{
{ 1,0,0 },
{ 2,0,0 },
{ 3,0,0 },
{ 4,0,0 },
{ 5,0,0 },
{ 6,0,0 },
};

##### Share on other sites
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 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}; // creatingmemcpy( RoomExits[0], newRow, 6*sizeof(int) );//change the row a littlenewRow[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 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 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 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 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 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 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 arrayint** Rooms=0;Rooms = new int*[down];for(int j=0;j

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631428
• Total Posts
3000027
×