[SOLVED]2D array assignment(C++)

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Donutslayer7
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Tue Aug 18, 2009 5:16 pm
Current Project: Map-Editor
Favorite Gaming Platforms: N64, SNES, anything Nintendo
Programming Language of Choice: C++
Location: U.S.

[SOLVED]2D array assignment(C++)

Post by Donutslayer7 »

A pretty basic question, I know: when I declare a 2D array...

Code: Select all

int map[5][5] =
{
          {0,0,0,0,0},
          {0,0,0,0,0},
          {0,0,0,0,0},
          {0,0,0,0,0},
          {0,0,0,0,0}
}
the code is fine, when I assign part of an array...

Code: Select all

map[3][2] = 1;
but I want to know if there is a way I can assign all of an arrays values at once much like declaring it...

Code: Select all

map[5][5] = //this doesn't work
{
...
}
I know I can use a for loop to loop through the array, but I just want to know if it's possible to assign it all at once
Last edited by Donutslayer7 on Sun Feb 28, 2010 12:05 am, edited 1 time in total.
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: 2D array assignment(C++)

Post by Bakkon »

I'm about 100% sure there's not a shorthand way to do it and you're required to loop through each element. The only way to assign a value to everything at once is when it's first defined, such as:

Code: Select all

int array[25] = {0};
This will initialize each element to 0, but there's no way to do something like that after it's already defined.

edit: Clarification, if you did this:

Code: Select all

int array[25] = {1};
It will initialize element [0] to 1 and the rest of the elements to 0.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: 2D array assignment(C++)

Post by GroundUpEngine »

Bakkon wrote:I'm about 100% sure there's not a shorthand way to do it and you're required to loop through each element.
That's the way I learned too ;)

Code: Select all

-- e.g.

for(int i = 0; i < 5; i++)
{
    for(int j = 0; j < 5; j++)
    {
         map[i][j] = 0;
    }
}
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: 2D array assignment(C++)

Post by avansc »

calloc
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: 2D array assignment(C++)

Post by qpHalcy0n »

You probably would not want to calloc. You'd want to memset in this case.
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: 2D array assignment(C++)

Post by RyanPridgeon »

You could create a seperate array such as

Code: Select all

int preset[3][3] = {
  { 0, 0, 0 },
  { 0, 1, 2 },
  { 1, 1, 2 }
};
or whatever, then use memcpy(), or your own function to copy the preset array into the array you're working with, such as

Code: Select all

int level[3][3];
memcpy(level, preset, sizeof(preset));
Which will copy it to be identical to your preset array, not matter where you are in the code (as long as preset is in scope).
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: 2D array assignment(C++)

Post by avansc »

^^, yeah if the mem was already allocated, and if you wanted anything other than 0.
but yeah, memset would be better.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: 2D array assignment(C++)

Post by eatcomics »

Or you could use a for loop, like the others said... But I guess you can do a memset
Image
User avatar
Donutslayer7
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Tue Aug 18, 2009 5:16 pm
Current Project: Map-Editor
Favorite Gaming Platforms: N64, SNES, anything Nintendo
Programming Language of Choice: C++
Location: U.S.

Re: 2D array assignment(C++)

Post by Donutslayer7 »

ah, copying a previous array, great idea, thanks, I'll look into this memset function
Post Reply