PDA

View Full Version : [C++] a little help here


mrvanx
3rd April 2007, 11:31 PM
Hey people, I may be being a bit newb-ish here but I just cant get how this is done:

(Code is C++)

Im trying to fill a 3d array with values, for readability i am addressing the 3rd dimension of it manually but i have written the following. The syntax is identical to a 2d array (a 2d array of size 4x2).

int pattern1[11][4][2],pattern2[11][4][2];

...

pattern1[0]={{-1,0},{-2,-1},{-3,-2},{-4,-3}};
pattern1[1]={{-1,-1},{-2,-2},{-3,-3},{-4,-4}};
pattern1[2]={{0,-1},{-1,-2},{-2,-3},{-3,-4}};
pattern1[3]={{0,-1},{0,-2},{-1,-3},{-2,-4}};
pattern1[4]={{0,-1},{0,-2},{0,-3},{-1,-4}};
pattern1[5]={{0,-1},{0,-2},{0,-3},{0,-4}};
pattern1[6]={{0,-1},{0,-2},{0,-3},{1,-4}};
pattern1[7]={{0,-1},{0,-2},{1,-3},{2,-4}};
pattern1[8]={{0,-1},{1,-2},{2,-3},{3,-4}};
pattern1[9]={{1,-1},{2,-2},{3,-3},{4,-4}};
pattern1[10]={{1,0},{2,-1},{3,-2},{4,-3}};

pattern2[0]={{-1,-1},{-1,-2},{-1,-3},{-1,-4}};
pattern2[1]={{0,-1},{0,-2},{0,-3},{0,-4}};
pattern2[2]={{1,-1},{1,-2},{1,-3},{1,-4}};
pattern2[3]={{1,-1},{2,-2},{2,-3},{2,-4}};
pattern2[4]={{1,-1},{2,-2},{3,-3},{3,-4}};
pattern2[5]={{1,-1},{2,-2},{3,-3},{4,-4}};
pattern2[6]={{1,-1},{2,-2},{3,-3},{4,-3}};
pattern2[7]={{1,-1},{2,-2},{3,-2},{4,-2}};
pattern2[8]={{1,-1},{2,-1},{3,-1},{4,-1}};
pattern2[9]={{1,0},{2,0},{3,0},{4,0}};
pattern2[10]={{1,1},{2,1},{3,1},{4,1}};


However i am getting compiler errors!?! Is this the correct way to assign values or am i waaay off??

levenum
4th April 2007, 05:45 PM
You can only do this kind of multiple initialization during variable declaration.
Later on in code you have to assign each component of the array seperatly, or use pointer tricks, memcpy etc.

So had you wrote something like:

int pattern1[11][4][2] = {{{1, 2}...

It might have worked.

Rudegar
4th April 2007, 09:13 PM
for such things it's generaly a better path to init it with the content of
a file or DB

hardcoded init's are bad in general