Code: Select all
typedef enum { Pawn, ... King } ChessSquare;
typedef ChessSquare Board[8][8];
Board boards[500];
void Apply(Board board)
{
int p = board - boards[0];
printf("%d\n", p);
}
Moderator: Ras
Code: Select all
typedef enum { Pawn, ... King } ChessSquare;
typedef ChessSquare Board[8][8];
Board boards[500];
void Apply(Board board)
{
int p = board - boards[0];
printf("%d\n", p);
}
That would depend on the contents of board[1] and board[0]hgm wrote:Suppose I have
When I now call Apply(boards[1]), is there any particular reason why this would print 16, rather then 1?Code: Select all
typedef enum { Pawn, ... King } ChessSquare; typedef ChessSquare Board[8][8]; Board boards[500]; void Apply(Board board) { int p = board - boards[0]; printf("%d\n", p); }
hgm wrote:Suppose I have
When I now call Apply(boards[1]), is there any particular reason why this would print 16, rather then 1?Code: Select all
typedef enum { Pawn, ... King } ChessSquare; typedef ChessSquare Board[8][8]; Board boards[500]; void Apply(Board board) { int p = board - boards[0]; printf("%d\n", p); }
I think you are mixing pointers with different sizes.hgm wrote:Suppose I have
When I now call Apply(boards[1]), is there any particular reason why this would print 16, rather then 1?Code: Select all
typedef enum { Pawn, ... King } ChessSquare; typedef ChessSquare Board[8][8]; Board boards[500]; void Apply(Board board) { int p = board - boards[0]; printf("%d\n", p); }
Code: Select all
int p = (ChessSquare*)board - (ChessSquare*)boards[0] -> 64
int p = (Board*)board - (Board*)boards[0] -> 1
So you are saying that I am mixing up pointers after all (although not within the expression): Although board and boards[0] are pointers, they are not (Board *), but they point to an 8-byte row of ChessSquare. That would explain the 8 (but not the 16).Karlo Bala wrote: For example:Code: Select all
int p = (ChessSquare*)board - (ChessSquare*)boards[0] -> 64 int p = (Board*)board - (Board*)boards[0] -> 1
Code: Select all
typedef enum { Pawn, King } ChessSquare;
typedef ChessSquare Board[8][8];
Board boards[500];
void Apply(Board *board)
{
int p = board - boards;
printf("%d\n", p);
}
int main()
{
Apply(&boards[1]);
return 0;
}
I think that problem arise after passing array as function parameter.hgm wrote:So you are saying that I am mixing up pointers after all (although not within the expression): Although board and boards[0] are pointers, they are not (Board *), but they point to an 8-byte row of ChessSquare. That would explain the 8 (but not the 16).Karlo Bala wrote: For example:Code: Select all
int p = (ChessSquare*)board - (ChessSquare*)boards[0] -> 64 int p = (Board*)board - (Board*)boards[0] -> 1
I was pretty sure I got all multiples of 16 (but in the mean time I fixed it by writing
p = ((int) board - (int)boards[0]) / ((int) boards[1] - (int) boards[0]);
(This seemed the safest, given that I didn't understand what was going on).
Code: Select all
004113BE mov eax,dword ptr [board]
004113C1 sub eax,offset boards (417178h)
004113C6 sar eax,5
004113C9 mov dword ptr [p],eax
Code: Select all
typedef ChessSquare Row[8];
int p = (Row*)board - (Row*)boards[0];