Advertisement

NEWBIE - 2-dimensional array question.

Started by November 24, 2000 09:11 AM
8 comments, last by Gollum 24 years, 1 month ago
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
Like this...

int exists[6][3] =
{
{ 1,0,0 },
{ 2,0,0 },
{ 3,0,0 },
{ 4,0,0 },
{ 5,0,0 },
{ 6,0,0 },
};
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
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
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
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
Advertisement
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
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.
Alexander "DmGoober" Jhinalexjh@online.microsoft.com[Warning! This email account is not attended. All comments are the opinions of an individual employee and are not representative of Microsoft Corporation.]
*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
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<down;j++)Rooms[j] = new int[across];//This replaces "a row of rooms"//not exactly, but kinda how you wanted toint* temp;int* NewRowOfRooms = new int[across];temp = Rooms[j];Rooms[j] = NewRowOfRooms;delete[] temp;//This destroys a dynamic 2D arrayfor(j=0;j<down;j++)delete[] Rooms[j];delete[] Rooms;  
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement