Re: for loops & 2d array
On Apr 8, 6:47 pm, "}{" <a...@b.com> wrote:
[snip]
map[(x-1),y)
[snip]
map[5,2] = 0 //which is correct
map[4,2] = 0 //it should be 255.
[snip]
Let me explain what's going on here.
map[5,2]
is, for build-in arrays, exactly equivalent to
* ( (map) + (5,2) )
The syntax "x[y]" for built-in arrays is exactly equivalent to "*((x)+
(y))".
The sub-expression "(5,2)" is parsed as two integer literals, the
comma operator acting on them, inside of parenthesis.
The comma operator
-first- evaluates the first part, the integer literal 5,
-then- evaluates the second part, the integer literal 2,
-then- returns the evaluated second part, the integer 2.
* ( (map) + (5,2) )
is equivalent to
* ( (map) + (2) )
is equivalent to
map[2]
Or more simply,
map[5,2]
is equivalent to
map[2]
The above is to be crystal clear.
What you want is something more like:
BYTE (*map)[10] = new BYTE [10] [10];
//...
map[5][2] = 0;
Or possibly:
BYTE *map = new BYTE [10 * 10];
//...
map[5*10 + 2] = 0; //stay consistent in your offset calculations
Or if reasonable, avoid the built-in arrays and use std::vector.
//This initializes x to contain 10 copies of the second argument,
//and the second argument is a vector containing 10 BYTEs.
vector<vector<BYTE> > x(10, std::vector<BYTE>(10));
x[5][2] = 0;
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]